Help with C nested structs

Asked

Viewed 317 times

1

I was creating in C a list and using a struct, after finishing I tried to see what changed with a struct nestled, created two structs basic, one receiving only one value and another receiving this first and a pointer for the same type.

Then I started to have a problem in entering the data, in this my list there are two methods of entering data, one that inserts at the beginning of the list and another that inserts at the end of the list, both worked in the same way (with its particularities of course) and I realized that when inserting a data it received, but when inserting the second value it changed the first data to be equal to the second and so on always changing all to the last entered data (it would be a pointer problem?)

I then took the method of inserting at the beginning and changed its structure to see if I could find another way to solve the problem, finally I created another problem, follow the link with the code: List in C

Note 1: Option 3 you enter at the end causes the problem I reported.

Edit: 24/05/16 19:25

About function insere_inicio(): It is working as it should, that is, by inserting distinct values, and how I will adopt for the function insere_final(), however I have another problem that I am not knowing how to solve, I would like to create chains of chars (strings) however I am not succeeding, because if I do:

scanf(" %s", &novo->person.name);

Causes a ERROR in the program, so I had to replace everything to %c, however I would like to read an entire name and not only a letter.

  • As for repeating the data: you are inserting the pointer into the list structures. Each structure has a pointer with the same address. When you input the new name, all pointers point to that name. You need to reserve space for each name within each structure.

  • Okay, so I’m not actually creating a new datum, but working with it just, is that it? But to solve this I will have to create a function to allocate a new space, I’m not very good with pointers yet, so I would actually like a help on how to proceed with this.

  • Rewriting and'always leave the code along with the question - not on external services like Pastebin

  • @jsbueno did not know, and as the code is a bit extensive I chose to use a service specialized in it not to "pollute" too much and be able to better describe the problem that was happening, but I was able to solve and in the same link is the code working (and its versions).

  • OK - the suegstao now is to search around, or create new questions - after all, you made it work but did not understand why: I think it is worth a new question! :-)

  • @jsbueno de facto, I know it works and understand superficially what is happening, however I do not even know what would be correct to ask about it, I understand that it was a problem with pointers that were not being updated (If you see the code, you will notice that to solve I chose to "dismember" the function allocates() in the part that receives the value), but I fully agree with your point, after all that it is worth having the code working without understanding what led to the error? I thank you for your willingness and help.

Show 1 more comment

1 answer

0


Problem solved. Link: Properly functioning solution I chose to use an array of char instead of a pointer (I confess that I do not know what changes between one and the other in this use).

I created a method called "aloca()" that is now receiving the information from the first struct and then correctly inserting the data into the second (main) struct, revealing that the problem was merely a pointer that was being manipulated directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.