I have taken a look at your code, I amnot really sure, but I found that your code would output something different from what you posted here ...?!
Your problem might be at the display function and the variable loc though....I think, if possible, you should be a litle more specific so that people here could give your some help.
I am sorry I should have attached the programme which I have attached now. The issue is.....
In the structure there are two variables one is character pointer and a double array.
What ever I store in the double array gets printed perfectly. But not in the character. What ever is stored in the character variable at last is repeated until the end of the list when I print it.
Oh well, I guess your problem is at your loop. You displayed after you finish your loop, so the variable j will contain the final value.
That is why your program output the same number after NM.
This is not really difficult problem, and it is advisable for you to correct this mistake yourself ! I am also sure you can make it !Good luck !!!
What you are doing here is just copying the address of the string nm into temp->name. If you change the string in nm, you are also changing the value pointed by temp->name. At the end all temp->name in every node will point to the same string. What you have to do is to reserve some space for the string with malloc and then copy the string char by char:
Originally posted by KabalProg
What you have to do is to reserve some space for the string with malloc and then copy the string char by char:
Ask for memory for the string:
temp->name=(char*) malloc(strlen(nm)+1);
Copy the string:
strcpy(temp->name, nm);
And that's it
Hope this helped
Kabalprog
Actually, that's not it. For every malloc() you have in your
program, there should be a corresponding free(). If you don't
know this already, then look up malloc() in your compiler's
documentation.
Since you gave a .c file, I assume you're writing a C program and
asking questions about it here [in a C++ forum]. That's fine. I
just wanted to make the point that if you're using C++, it is
recommended that you use new and delete instead of malloc()
and free(). For YOUR particular case it's probably not an issue,
but it's for those people who read this post as a result of a google
search four years from now
I actually work in a API related to UNIGRAPHICS a CAD/CAM/CAE package. Its API help and examples are rich in C but not in C++. Thats why I am using C. Any how once I master its API I will definitely switch to C++. Thanks for your intrest and time spent.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.