Could you please help me? what is wrong with this code? it prints only D->D->D->D (I want A->B->C->D)
struct node / Declaration of structure "node" / { char letter[1]; struct node *next ; };
struct node *start = NULL;
main(void) { struct node current = start; struct node new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node)); new_ptr->next = NULL; strcpy(new_ptr->letter, "A"); start = new_ptr; current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node)); new_ptr->next = NULL; strcpy(new_ptr->letter, "B"); current->next = new_ptr; current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node)); new_ptr->next = NULL; strcpy(new_ptr->letter, "C"); current->next = new_ptr; current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node)); new_ptr->next = NULL; strcpy(new_ptr->letter, "D"); current->next = new_ptr; current = new_ptr;
current = start; printf("%s->\n ",new_ptr->letter); while(current != NULL) { printf("%s-> ",new_ptr->letter); current = current->next; } system("PAUSE"); }
your problem is in your while loop
/***/ what is my pointer pointing to does that ever change is my pointer ever pointing to anything else
you get the drift...I would just tell you but this may be homework /***/
while(current != NULL) { printf("%s-> ",new_ptr->letter); current = current->next; }
Log in to post a comment.
Could you please help me?
what is wrong with this code?
it prints only D->D->D->D (I want A->B->C->D)
struct node / Declaration of structure "node" /
{
char letter[1];
struct node *next ;
};
struct node *start = NULL;
main(void)
{
struct node current = start;
struct node new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node));
new_ptr->next = NULL;
strcpy(new_ptr->letter, "A");
start = new_ptr;
current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node));
new_ptr->next = NULL;
strcpy(new_ptr->letter, "B");
current->next = new_ptr;
current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node));
new_ptr->next = NULL;
strcpy(new_ptr->letter, "C");
current->next = new_ptr;
current = new_ptr;
new_ptr = (struct node *)malloc(sizeof(struct node));
new_ptr->next = NULL;
strcpy(new_ptr->letter, "D");
current->next = new_ptr;
current = new_ptr;
current = start;
printf("%s->\n ",new_ptr->letter);
while(current != NULL)
{
printf("%s-> ",new_ptr->letter);
current = current->next;
}
system("PAUSE");
}
your problem is in your while loop
/***/
what is my pointer pointing to
does that ever change
is my pointer ever pointing to anything else
you get the drift...I would just tell you but this may be homework
/***/
while(current != NULL)
{
printf("%s-> ",new_ptr->letter);
current = current->next;
}