Passing structs

What I need to do, is for every line of my input (which is unknown until runtime), I need to create a struct and pass the struct to a list. To function to append stuff to the end of a list is dll_append(Dllist *list, void *node_value).

What keeps confusing me, is how to make a different struct for every line in the file. Furthermore, even if I just pass one struct and then try to extract say a name field of the struct, I just get random characters.

In one struct you have
1
2
3
typedef struct myStruct {
    struct myStruct* next; /* Pointer to the next instance of myStruct */
}   myStruct_t;

Is that the kind of thing you want? Google-search for linked lists; I think that is what you want.
I don't have a problem with the list thing. Technically I could use an array, but lists are easier because I have to sort it later.

Basically, I'm reading in data like so:
Name, age, height , eye color, etc

So I need a struct to hold all the info for each person per line. Now, I can pass one struct ok, but if I have multiple lines of input, I just keep writing over my previous struct. Furthermore, whenever I attempt to pull my struct back out of the list, I just get random characters (which is probably a pointer problem - I'm more concerned with the first issue).

@chrisname - Something like that might work, I'll try it out.
Are you using C or C++?
I would like to use C. Hence the not using classes.
Last edited on
I would make a function called "GetLine()" or something similar, which takes a FILE* and returns a myStruct* (or you could pass it in if you wanted), and then reads enough information from the file.
Well, I'm using a get_line to read in the lines, but it's not my library. It returns a struct that allows me to grab each word in the line through char *token = strdup(input_struct->fields[i]). But, once again, it only gets written over with every call of get_line(input).

What I want is something like this:
1
2
3
4
5
6
7
8
9
10
11
int main()
//blah blah

while(get_line(input))
{       
         myStruct.name = strdup(input->fields[0]);
         myStruct.age = atoi(input->fields[1]);
         //etc
         
         dll_append(list, myStruct);
}


But with a different struct for each person.
Last edited on
Move to the next list node at each iteration of the loop
Topic archived. No new replies allowed.