Problem with pointers

J

Javier

Hi All

I've the defined the following structure:

struct PhoneNumbers
{
char *numero;
struct PhoneNumbers *next;
};

PhoneNumbers *phones = NULL;
PhoneNumbers *primero = NULL;
PhoneNumbers *prov = NULL;


Then I read a text file and fill the linked list with data:

while (! examplefile.eof() ) {

examplefile.getline (buffer,100);

if (primero == NULL) {
primero = new PhoneNumbers;
phones = primero;
phones->next = NULL;
} else {
phones->next = new PhoneNumbers;
phones = phones->next;
phones->next = NULL;
}

phones->numero = buffer;

}



Everything goes well till now but...

After this, I want to verify if my linked list was filled correctly with
data. Then I did:



cout << "\nPrimero " << primero;
printf("\n-----------------\n");

phones = primero;
while (phones != NULL) {
cout << "Ptr: " << phones << " Numero: " << phones->numero << endl;
phones = phones->next;
}


But I see only the last data read repeated in all nodes !!!

I'm new with C++ and I couldn't find my mistake... I don't know if the
error comes from filling procedure or when I try to show data.

Any help appreciated.

Javier
 
R

red floyd

Javier said:
Hi All

[redacted]
Then I read a text file and fill the linked list with data:

while (! examplefile.eof() ) {

examplefile.getline (buffer,100);

if (primero == NULL) {
primero = new PhoneNumbers;
phones = primero;
phones->next = NULL;
} else {
phones->next = new PhoneNumbers;
phones = phones->next;
phones->next = NULL;
}

phones->numero = buffer;

}
[redacted]

You have made one of the classic blunders with iostreams (the other
being "never get involved in a land war in Asia"). You don't write a
loop like that. It should read:

while (examplefile.getline(buffer,100))
{
// do stuff
}
 
R

red floyd

Javier said:
Hi All

I've the defined the following structure:

struct PhoneNumbers
{
char *numero;
struct PhoneNumbers *next;
};

PhoneNumbers *phones = NULL;
PhoneNumbers *primero = NULL;
PhoneNumbers *prov = NULL;


Then I read a text file and fill the linked list with data:

while (! examplefile.eof() ) {

examplefile.getline (buffer,100);

if (primero == NULL) {
primero = new PhoneNumbers;
phones = primero;
phones->next = NULL;
} else {
phones->next = new PhoneNumbers;
phones = phones->next;
phones->next = NULL;
}

phones->numero = buffer;

}
In addition, I'm assuming buffer is a array of 100 char (char buffer[100]).

IN all cases, phones->numero all point to the same buffer. You never
copy it out, so of course you will see nothing but the same number.

Hint: use std:string for your getline and in your PhoneNumbers.
Also, use a constructor, so you don't have to explicitly set the next
field to NULL.
Also, use std::list instead... unless you're trying to understand how
the linked list data structure works, in which case rolling your own is
a good idea.
 
A

Andrew Koenig

Javier said:
Hi All

I've the defined the following structure:

struct PhoneNumbers
{
char *numero;
struct PhoneNumbers *next;
};

PhoneNumbers *phones = NULL;
PhoneNumbers *primero = NULL;
PhoneNumbers *prov = NULL;


Then I read a text file and fill the linked list with data:

while (! examplefile.eof() ) {

examplefile.getline (buffer,100);

if (primero == NULL) {
primero = new PhoneNumbers;
phones = primero;
phones->next = NULL; } else {
phones->next = new PhoneNumbers;
phones = phones->next;
phones->next = NULL;
}
phones->numero = buffer;

}

Your fundamental problem is that you're storing a bunch of pointers to the
same buffer, which changes every time you read a line. So of course you
will print some number of copies of the last line you read and nothing else.

I'm not going to try to fix the code, because I think you're going down the
wrong track entirely. I can see no reason why you should write something
like this instead:

std::vector<std::string> phones;

for (std::string line; std::getline(examplefile, line); )
phones.push_back(line);

Now you don't have to worry about all this pointer fiddling.
 
O

oichi

// allocate mem to create new object to store the data
phones->numero = new ( char [ sizeof (buffer)] );
strcpy(phones->numero, buffering);
//phones->numero = buffer;

}



Everything goes well till now but...

After this, I want to verify if my linked list was filled correctly with
data. Then I did:



cout << "\nPrimero " << primero;
printf("\n-----------------\n");

phones = primero;
while (phones != NULL) {
cout << "Ptr: " << phones << " Numero: " << phones->numero << endl; delete phones->numero;
phones = phones->next;
}
char array
 
O

oichi

// allocate mem to create new object to store the data
phones->numero = new ( char [ sizeof (buffer)] );
strcpy(phones->numero, buffering);
//phones->numero = buffer;

}



Everything goes well till now but...

After this, I want to verify if my linked list was filled correctly with
data. Then I did:



cout << "\nPrimero " << primero;
printf("\n-----------------\n");

phones = primero;
while (phones != NULL) {
cout << "Ptr: " << phones << " Numero: " << phones->numero << endl; delete phones->numero;
phones = phones->next;
}
char array
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,186
Messages
2,570,997
Members
47,586
Latest member
Gilda57E93

Latest Threads

Top