0

I made a small program where I have a struct and filling its parameter based on value from console.

#define M 3
struct data {
    char* info;
    int key;
};

void initHash(data* d) {
    int i;
    for(i=0; i<M; i++) {
        d[i].info = " ";
        d[i].key = -1;
    }
}


void printList(data* d) {
    int i;
    for(i=0; i< M; i++) {
        if(strcmp(d[i].info, " ")) {
            printf(" %d %d %s \n", i, d[i].key, d[i].info);
        }
    }
}

int linearProbing() {
    struct data d[M];
    int hashval;
    char* info;
    char str[25];


    initHash(d);
    scanf("%s",&str);
    while(strcmp(str, "end") != 0) {
    d[id].info = str;
    **printf("before reading \n");
    printList(d);
    id++;
    scanf("%s",&str);
    printf("after reading \n");
    printList(d);
    printf("next iter \n");**
    }
    printList(d);
    return true;
}

My issue is in the highlighted lines the first print function prints correct value but after getting input , the data.info param changes to the new input value for all the M members of struct array. Can someone explain why it might be so?

Sample output:

statue
before reading 
 0 -1 statue 
tornado
after reading 
 0 -1 tornado 
next iter 
before reading 
 0 -1 tornado 
 1 -1 tornado 
clown
after reading 
 0 -1 clown 
 1 -1 clown 
next iter 
before reading 
 0 -1 clown 
 1 -1 clown 
 2 -1 clown
tariq zafar
  • 659
  • 7
  • 24
  • 1
    Are you even able to compile this? I can see a lot of bugs. – digital_revenant Aug 21 '13 at 13:12
  • Looks like C++ to me? – James M Aug 21 '13 at 13:13
  • Yup, should be tagged under C++ – digital_revenant Aug 21 '13 at 13:13
  • What? Where do you see any c++? Am I blind? 'cause I don't see any c++ line in there, however I do recognize plenty c-style stuff. `c++` would be miss-tagging it IMO. – luk32 Aug 21 '13 at 13:17
  • Variable declaration inside `for` loop isn't allowed in C. Also, variables are declared in the code using `data` instead of `struct data`. – digital_revenant Aug 21 '13 at 13:20
  • Sounds like C compiled on a C++ compiler (because that's clearly C style). – Medinoc Aug 21 '13 at 13:25
  • My mistake, I might have compiled using C++ compiler in eclipse. However, I forgot to miss on those two points. But still haven't used any core C++ stuff in it. I'll edit the code to make it strictly C. – tariq zafar Aug 21 '13 at 13:28
  • @Kunal you can declare variables like that `c` (at least on modern version) by using `typedef` when declaring struct. I think calling code `c++`-like only cause of a in-loop variable declaration with all those `printf`s `scanf`s and OOP implementated using `struct` + functions operating on pointers to those structs is not a bold statement, but a mistake. But I give you that `c` compiler would fail to compile it and `c++` would! – luk32 Aug 21 '13 at 13:35
  • `typedef` was my first thought too, but that was missing in the code. – digital_revenant Aug 21 '13 at 13:40
  • 1
    Please get a real C compiler like GCC, compile the code, fix all errors then repost. For example `gcc -std=c99 -pedantic-errors -Wall` – Lundin Aug 21 '13 at 13:54

2 Answers2

4

You're not copying the input strings to each d[id].info member - you're setting those pointers to the same character buffer (str), which you're using and overwriting again and again.

That is, d[0].info, d[1].info, etc. all point to the same memory.

You likely want to say:

d[id].info = strdup(str);

to point to a new copy of the string each time.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

You are making the info variable point to a string literal. In order to store a string, you should first allocate sufficient storage like:

info = (char *) malloc(string size * sizeof(char))

and then copy your string using strcpy

digital_revenant
  • 3,274
  • 1
  • 15
  • 24