C Linked List char array input reuse problem
up vote
1
down vote
favorite
typedef struct NODE
char *word;
struct NODE *next;
node;
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = word;
pNode->next = NULL;
return pNode;
void append(node **ppList, char *word)
if(*ppList == NULL)
*ppList = newNode(word);
else
node *tmpList = *ppList;
for(; tmpList->next!=NULL; tmpList=tmpList->next);
tmpList->next = newNode(word);
void printList(node *list)
for(; list!=NULL; list=list->next)
printf("[%s]=>", list->word);
printf("NULL");
/*=== CODE 1 ===*/
int main()
char word[MAXCHAR], word2[MAXCHAR], word3[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word2); /* key in BBB */
append(&list, word2);
scanf("%s", &word3); /* key in CCC */
append(&list, word3);
printList(list);
return 0;
/*=== CODE 2 ===*/
int main()
char word[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word); /* key in BBB */
append(&list, word);
scanf("%s", &word); /* key in CCC */
append(&list, word);
printList(list);
return 0;
The outputs:
=== CODE 1 OUTPUT ===
[AAA]=>[BBB]=>[CCC]=>NULL /* it works */
=== CODE 2 OUTPUT ===
[CCC]=>[CCC]=>[CCC]=>NULL /* doesnt work, why? */
Hi, I am trying to loop this thing then I realized it got a wrong result. I isolated my program and I found out that the input is the problem, I tried scanf and gets both doesnt work. Why cant I use back the char array to store the input, could someone help me with this please.
c arrays input linked-list char
add a comment |
up vote
1
down vote
favorite
typedef struct NODE
char *word;
struct NODE *next;
node;
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = word;
pNode->next = NULL;
return pNode;
void append(node **ppList, char *word)
if(*ppList == NULL)
*ppList = newNode(word);
else
node *tmpList = *ppList;
for(; tmpList->next!=NULL; tmpList=tmpList->next);
tmpList->next = newNode(word);
void printList(node *list)
for(; list!=NULL; list=list->next)
printf("[%s]=>", list->word);
printf("NULL");
/*=== CODE 1 ===*/
int main()
char word[MAXCHAR], word2[MAXCHAR], word3[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word2); /* key in BBB */
append(&list, word2);
scanf("%s", &word3); /* key in CCC */
append(&list, word3);
printList(list);
return 0;
/*=== CODE 2 ===*/
int main()
char word[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word); /* key in BBB */
append(&list, word);
scanf("%s", &word); /* key in CCC */
append(&list, word);
printList(list);
return 0;
The outputs:
=== CODE 1 OUTPUT ===
[AAA]=>[BBB]=>[CCC]=>NULL /* it works */
=== CODE 2 OUTPUT ===
[CCC]=>[CCC]=>[CCC]=>NULL /* doesnt work, why? */
Hi, I am trying to loop this thing then I realized it got a wrong result. I isolated my program and I found out that the input is the problem, I tried scanf and gets both doesnt work. Why cant I use back the char array to store the input, could someone help me with this please.
c arrays input linked-list char
2
You need to show howappendfunction is defined and please provide Minimal, Complete, and Verifiable example.
– kiran Biradar
Nov 10 at 6:52
scanf("%s", &word);Instead of this, try this:scanf("%s", word);
– kiner_shah
Nov 10 at 6:52
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
typedef struct NODE
char *word;
struct NODE *next;
node;
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = word;
pNode->next = NULL;
return pNode;
void append(node **ppList, char *word)
if(*ppList == NULL)
*ppList = newNode(word);
else
node *tmpList = *ppList;
for(; tmpList->next!=NULL; tmpList=tmpList->next);
tmpList->next = newNode(word);
void printList(node *list)
for(; list!=NULL; list=list->next)
printf("[%s]=>", list->word);
printf("NULL");
/*=== CODE 1 ===*/
int main()
char word[MAXCHAR], word2[MAXCHAR], word3[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word2); /* key in BBB */
append(&list, word2);
scanf("%s", &word3); /* key in CCC */
append(&list, word3);
printList(list);
return 0;
/*=== CODE 2 ===*/
int main()
char word[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word); /* key in BBB */
append(&list, word);
scanf("%s", &word); /* key in CCC */
append(&list, word);
printList(list);
return 0;
The outputs:
=== CODE 1 OUTPUT ===
[AAA]=>[BBB]=>[CCC]=>NULL /* it works */
=== CODE 2 OUTPUT ===
[CCC]=>[CCC]=>[CCC]=>NULL /* doesnt work, why? */
Hi, I am trying to loop this thing then I realized it got a wrong result. I isolated my program and I found out that the input is the problem, I tried scanf and gets both doesnt work. Why cant I use back the char array to store the input, could someone help me with this please.
c arrays input linked-list char
typedef struct NODE
char *word;
struct NODE *next;
node;
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = word;
pNode->next = NULL;
return pNode;
void append(node **ppList, char *word)
if(*ppList == NULL)
*ppList = newNode(word);
else
node *tmpList = *ppList;
for(; tmpList->next!=NULL; tmpList=tmpList->next);
tmpList->next = newNode(word);
void printList(node *list)
for(; list!=NULL; list=list->next)
printf("[%s]=>", list->word);
printf("NULL");
/*=== CODE 1 ===*/
int main()
char word[MAXCHAR], word2[MAXCHAR], word3[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word2); /* key in BBB */
append(&list, word2);
scanf("%s", &word3); /* key in CCC */
append(&list, word3);
printList(list);
return 0;
/*=== CODE 2 ===*/
int main()
char word[MAXCHAR];
node *list=NULL;
scanf("%s", &word); /* key in AAA */
append(&list, word);
scanf("%s", &word); /* key in BBB */
append(&list, word);
scanf("%s", &word); /* key in CCC */
append(&list, word);
printList(list);
return 0;
The outputs:
=== CODE 1 OUTPUT ===
[AAA]=>[BBB]=>[CCC]=>NULL /* it works */
=== CODE 2 OUTPUT ===
[CCC]=>[CCC]=>[CCC]=>NULL /* doesnt work, why? */
Hi, I am trying to loop this thing then I realized it got a wrong result. I isolated my program and I found out that the input is the problem, I tried scanf and gets both doesnt work. Why cant I use back the char array to store the input, could someone help me with this please.
c arrays input linked-list char
c arrays input linked-list char
edited Nov 10 at 7:09
asked Nov 10 at 6:47
BKstackers
83
83
2
You need to show howappendfunction is defined and please provide Minimal, Complete, and Verifiable example.
– kiran Biradar
Nov 10 at 6:52
scanf("%s", &word);Instead of this, try this:scanf("%s", word);
– kiner_shah
Nov 10 at 6:52
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57
add a comment |
2
You need to show howappendfunction is defined and please provide Minimal, Complete, and Verifiable example.
– kiran Biradar
Nov 10 at 6:52
scanf("%s", &word);Instead of this, try this:scanf("%s", word);
– kiner_shah
Nov 10 at 6:52
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57
2
2
You need to show how
append function is defined and please provide Minimal, Complete, and Verifiable example.– kiran Biradar
Nov 10 at 6:52
You need to show how
append function is defined and please provide Minimal, Complete, and Verifiable example.– kiran Biradar
Nov 10 at 6:52
scanf("%s", &word); Instead of this, try this: scanf("%s", word);– kiner_shah
Nov 10 at 6:52
scanf("%s", &word); Instead of this, try this: scanf("%s", word);– kiner_shah
Nov 10 at 6:52
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The problem is you are assigning the pointer.
pNode->word = word;
Since pNode->word will always points to updated value of word in main. Each node in the list will have the same value.
You should copy the contents of word in main instead of assigning the pointer.
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = malloc(strlen(word)+1);
strcpy(pNode->word, word);
pNode->next = NULL;
return pNode;
Or
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = strdup(word);
pNode->next = NULL;
return pNode;
Note: strdup is not C standard.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The problem is you are assigning the pointer.
pNode->word = word;
Since pNode->word will always points to updated value of word in main. Each node in the list will have the same value.
You should copy the contents of word in main instead of assigning the pointer.
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = malloc(strlen(word)+1);
strcpy(pNode->word, word);
pNode->next = NULL;
return pNode;
Or
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = strdup(word);
pNode->next = NULL;
return pNode;
Note: strdup is not C standard.
add a comment |
up vote
0
down vote
accepted
The problem is you are assigning the pointer.
pNode->word = word;
Since pNode->word will always points to updated value of word in main. Each node in the list will have the same value.
You should copy the contents of word in main instead of assigning the pointer.
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = malloc(strlen(word)+1);
strcpy(pNode->word, word);
pNode->next = NULL;
return pNode;
Or
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = strdup(word);
pNode->next = NULL;
return pNode;
Note: strdup is not C standard.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The problem is you are assigning the pointer.
pNode->word = word;
Since pNode->word will always points to updated value of word in main. Each node in the list will have the same value.
You should copy the contents of word in main instead of assigning the pointer.
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = malloc(strlen(word)+1);
strcpy(pNode->word, word);
pNode->next = NULL;
return pNode;
Or
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = strdup(word);
pNode->next = NULL;
return pNode;
Note: strdup is not C standard.
The problem is you are assigning the pointer.
pNode->word = word;
Since pNode->word will always points to updated value of word in main. Each node in the list will have the same value.
You should copy the contents of word in main instead of assigning the pointer.
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = malloc(strlen(word)+1);
strcpy(pNode->word, word);
pNode->next = NULL;
return pNode;
Or
node *newNode(char *word)
node *pNode = (node*) malloc(sizeof(node));
pNode->word = strdup(word);
pNode->next = NULL;
return pNode;
Note: strdup is not C standard.
answered Nov 10 at 7:34
kiran Biradar
4,2802826
4,2802826
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236665%2fc-linked-list-char-array-input-reuse-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You need to show how
appendfunction is defined and please provide Minimal, Complete, and Verifiable example.– kiran Biradar
Nov 10 at 6:52
scanf("%s", &word);Instead of this, try this:scanf("%s", word);– kiner_shah
Nov 10 at 6:52
Voted to close because it is unclear what you are asking. Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– Swordfish
Nov 10 at 6:57