How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Ingo U Your Own Question

Ingo U
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience:  Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
44076068
Type Your Programming Question Here...
Ingo U is online now

This linked list in c works fine but not ,

Resolved Question:

This linked list in c works fine for integers but not for strings, it just keeps printing out the last node:
#include
struct node {
char *str;
struct node *next;
};
int main() {
char text[80];
struct node *root;
struct node *trav;
root = (struct node*)malloc(sizeof(struct node));
printf("Enter string: ");
scanf("%s", &text);
root -> str = text;
trav = root;
trav -> next = (struct node*)malloc(sizeof(struct node));
trav = trav -> next;
printf("Enter string: ");
scanf("%s", &text);
trav -> str = text;
trav -> next = 0;
trav = root;
while(trav != 0) {
printf("%s ", trav -> str);
trav = trav -> next;
}
return 0;
}
Submitted: 1 year ago.
Category: Programming
Expert:  Ingo U replied 1 year ago.
Hi,
you are re-using the same text buffer for both elements. Since each node stores a pointer to a string, they both point to the same string. Your second scanf call overwrites the contents of the text array, so both nodes look like they have the same string, the last one you entered.
You'll need to allocate a separate string buffer for each node you're going to use.
Regards,
Ingo U
Customer: replied 1 year ago.

I'd pay extra if you could tell me how I could flush the buffer effectively so I could reuse the same string variable (so I could use it in a loop)?

Expert:  Ingo U replied 1 year ago.
OK, I'll write some code to illustrate.
Expert:  Ingo U replied 1 year ago.
Here is a modified version of your file:
https://www.dropbox.com/s/ebfs0hv4x95n5pl/LinkedList.c?dl=0
It's still uploading at the moment, hopefully finished by the time you see this.
Let me know if I can be of further assistance. I imagine you might have other questions, I'm here to help.
regards,
Ingo U
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience: Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
Ingo U and other Programming Specialists are ready to help you
Customer: replied 1 year ago.

Thank you for the code you have written. It has answered my question. It just remains for me to rate you with the best rating.

Expert:  Ingo U replied 1 year ago.
Thanks for the positive rating! Please feel free to request me directly at
http://www.justanswer.com/computer/expert-ingou/
if you'd like to work with me on a future project.
Regards,
Ingo

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Previous | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5514
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    936
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    766
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    558
    Founder of StockCanvas.com
  • http://ww2.justanswer.com/uploads/FA/fael/2011-1-15_104041_profile500x500.64x64.jpg Rafael Martins's Avatar

    Rafael Martins

    Computer Software Engineer

    Satisfied Customers:

    446
    Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    320
    Extensive Knowledge in PHP, MYSQL, CSS & Javascript