C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 3rd, 2003, 02:40 AM
Herzarsen Herzarsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: MD, USA
Posts: 2 Herzarsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Herzarsen Send a message via AIM to Herzarsen Send a message via Yahoo to Herzarsen
Pointers to Strinks in Linked List (Please Help)

Basically I have a !doubly linked list, which uses structs. It reads in data from the file and stores it in the list. It all works perfectly when I use numbers or characters. But my problem is when I read in strings... I have to use C.

The code still runs fine, but at the end the last read in word overwrites all the ones stored before it. The pointers seem to be connected and thus all values in the set of pointers are changed every time. I dont know how to get around it. I know there must be a trick, and I have tried for two days but without any success.
Hope there is someone outthere who can help!

Thanks so much in advance!


............
char chars[100]; // Stores the read in word from the file
............
struct Word
{
struct Word * nextPtr;
struct Word * prevPtr;
char *x;
};
..............
Word *lastPtr; // The previous struct
Word *mainPtr; // The main struct into which a value is being entered
..............
Word *header; // Header

header = new Word;
lastPtr = header;

lastPtr->prevPtr=NULL;

if ((filePtr = fopen ("text.txt", "r")) == NULL)
printf ("Error, FILE could not be found!");
else
{

//Creating a new Nodes of type Struct for Input Data

while (!feof(filePtr))
{
mainPtr = new Word;

lastPtr->nextPtr=mainPtr;
//lastPtr->prevPtr=NULL;
mainPtr->prevPtr=lastPtr;
mainPtr->nextPtr=NULL;

fscanf (filePtr, "%s", chars); // Read in the word

mainPtr->x=chars; // Here is the problem i think... Once fscanf reads in the word all the previous strings in the previous structs are changed to this one...
lastPtr = mainPtr; // Make the last pointer equal to the main one
}

mainPtr = header->nextPtr; // Main pointer is set to heather, beggining pointer value

// Testing Data
while (mainPtr->nextPtr != NULL)
{
printf("Data : %d\n", mainPtr->x); // Prints out the pointer string value (thats the problem)
mainPtr = mainPtr->nextPtr;
printf(">Ptr : %d >\n", mainPtr); // Prints out the pointer value of pointer to the struct
}

>>>In the testing stage above, I get different pointer address values for the second printout but the first printout for data (x's values) returns same value for all ocurances. That is my problem!

Reply With Quote
  #2  
Old February 3rd, 2003, 02:12 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
Quote:
mainPtr->x=chars; // Here is the problem i think... Once fscanf reads in the word all the previous strings in the previous structs are changed to this one...


You were spot on; that's the statement with the problem. The thing is that with that statement, you're telling each element of the list to point to the same location, chars. But chars can only hold one string, obviously. What you need to do, is duplicate the string in chars to a new location, and let all the x attributes point to those locations.

Oh, and since you said you had to do it all in C, try getting rid of all the occurrences of new, and replace them with the appropriate calls to malloc(). new is C++, not C.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old February 4th, 2003, 04:15 AM
The Dog The Dog is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Cape Town SA
Posts: 3 The Dog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
One thing you should try to avoid is the use of feof() as a loop condition.

Instead of having this :

while (!feof(filePtr))
{
//...
}

Use the fscanf() call as the loop condition, like this :

while ( fscanf (filePtr, "%s", chars) != EOF )
{
//...
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Pointers to Strinks in Linked List (Please Help)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway