CodeNewbie Community 🌱

Christopher Cooper
Christopher Cooper

Posted on

Linked List in C

I am trying this code out of C for Dummies, but for some reason, it doesn't give any output, and I'm not sure why because it's the same as in the book. Can anyone see why this is happening?
int main(void){
struct jb{
char actor[25];
struct jb *next;
};

char *bonds[RECORDS]= {
    "Sean Connery"
    "David Niven"
    "George Lazenby"
    "Roger Moore"
    "Timothy Dolton"
    "Pierce Brosnan"
};

struct jb *first_item;
struct jb *current_item;
struct jb *new_item;
int index = 0;

// create first structure in the list
first_item = (struct jb *)malloc(sizeof(struct jb));
current_item = first_item;

//fill in the structure - while loop iteerrates across each item in the list
while(1){
    strcpy(current_item->actor,bonds[index]);

    index++;
    if(index < RECORDS){
        new_item = (struct jb *)malloc(sizeof(struct jb));
        current_item->next = new_item;
        current_item = new_item;
    } else {
        current_item->next = NULL;
        break;

    }
}

//display the results
current_item = first_item;  //start over
index = 1;

while(current_item){
    printf("Structure %d: ",index++);
    printf("%s \n",current_item->actor);
    current_item = current_item->next;
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}

/* Expected output
Structure 1: Sean Connery
Structure 2: David Niven
Structure 3: George Lazenby
Structure 4: Roger Moore
Structure 5: Timothy Dolton
Structure 6: Pierce Brosnan
*/

Top comments (1)

Collapse
 
djuber profile image
Daniel Uber

Hint: modify the program to print bonds[0], and then print bonds[1].