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.










share|improve this question



















  • 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










  • 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














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.










share|improve this question



















  • 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










  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 7:09

























asked Nov 10 at 6:47









BKstackers

83




83







  • 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










  • 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




    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










  • 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












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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 7:34









        kiran Biradar

        4,2802826




        4,2802826



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus