Structure's with strings and input

Posted by Beginnernato on Stack Overflow See other posts from Stack Overflow or by Beginnernato
Published on 2012-03-28T23:10:24Z Indexed on 2012/03/28 23:29 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

so i have the following structure and function that add's things to the function -

struct scoreentry_node {
    struct scoreentry_node *next;
    int score;
    char* name;    
}
;

typedef struct scoreentry_node *score_entry;

score_entry add(int in, char* n, score_entry en) {      
   score_entry r = malloc(sizeof(struct scoreentry_node));
   r->score = in;
   r->name = n;
   r->next = en;  
   return r;   
}

i have input that take it in the following main file:

int score;
char name[];

int main(void) {
score_entry readin = NULL;

while(1)
{

        scanf("%s%d", name, &score);
        readin = add(score, name, readin);
       // blah blah

I dont know why but when input a name it gets added to readin, but when i input another name all the name's in readin have this new name

for example:

input:
bob 10 
readin = 10 bob NULL

jill 20
readin = 20 jill 10 jill NULL

I dont know why bob disappear's... any reason why it does that ?

© Stack Overflow or respective owner

Related posts about c

    Related posts about string