I have problem in linked list. When I compile and run the below code I have getting the following out put.
NM9
10.000000,11.000000,12.000000,13.000000
NM9
11.000000,12.000000,13.000000,14.000000
NM9
12.000000,13.000000,14.000000,15.000000
NM9
13.000000,14.000000,15.000000,16.000000
NM9
14.000000,15.000000,16.000000,17.000000
NM9
15.000000,16.000000,17.000000,18.000000
NM9
16.000000,17.000000,18.000000,19.000000
NM9
17.000000,18.000000,19.000000,20.000000
NM9
18.000000,19.000000,20.000000,21.000000
NM9
19.000000,20.000000,21.000000,22.000000
Which I expected to be like this.
NM0
10.000000,11.000000,12.000000,13.000000
NM1
11.000000,12.000000,13.000000,14.000000
NM2
12.000000,13.000000,14.000000,15.000000
NM3
13.000000,14.000000,15.000000,16.000000
NM4
14.000000,15.000000,16.000000,17.000000
NM5
15.000000,16.000000,17.000000,18.000000
NM6
16.000000,17.000000,18.000000,19.000000
NM7
17.000000,18.000000,19.000000,20.000000
NM8
18.000000,19.000000,20.000000,21.000000
NM9
19.000000,20.000000,21.000000,22.000000
I have attached the code. Could any one please tell me what is wrong. I am getting the values correctly as I wanted but not the string.
Thanks and Regards,
Abbas
Wfranc
March 10th, 2003, 02:19 AM
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. :)
Regards,
am_abbas
March 10th, 2003, 02:33 AM
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.
NAME10
1
NAME10
2
NAME10
3
So on ......
Thanks and Regards,
Abbas
Wfranc
March 10th, 2003, 03:27 AM
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 !!!
Regards,
KabalProg
March 10th, 2003, 08:48 AM
Hi. The problem here is when you copy the string:
temp->name=nm;
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:
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
PaulWendt
March 10th, 2003, 09:40 AM
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 :)
--Paul
am_abbas
March 10th, 2003, 11:35 AM
Hi PaulWendt,
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.
Thanks and Best regards,
Abbas
KabalProg
March 10th, 2003, 11:45 AM
Yes, you are right, every time you use malloc you have to do a free. But you can do that when you are destroying the list.