0

I'm just starting to learn C, so if you can, please don't be so hard on me. I'm trying to learn how to allocate memory for arrays, so i started with someting like this. I just wanna dynamicly allocate memory for array of strings, and then display it.

int main( )
{
    int number, i;
    scanf ("%d", &number);
    char **table =(char **) malloc(number*sizeof(char*));
    for(i=0; i<number; i++)
    {
        table[i] = (char *)malloc(6);
    }

    for(i=0; i<number; i++)
    {
        scanf("%s", &table[i]);
    }

    for(i=0; i<number; i++)
    {
        printf("Person nr %d : %s ", i+1, &table[i]);
    }

    for(i=0; i<number; i++)
    {
         free(table[i]);
    }
    free(table);
    return 0;
}

But program only works when i type words with 3 or less letters. So, I don't know if i have a problem with memory allocation, or maybe i i just can't print with %s for **char? Maybe someone could tell me where I'm doing wrong and explain why?

Thank you for taking the time to read it :)

Lundin
  • 195,001
  • 40
  • 254
  • 396
Leonek
  • 31
  • 2
  • 1
    Do not cast malloc. Replace `char **table =(char **) malloc(number*sizeof(char*))` with `char **table = malloc(number*sizeof(char*))` and `table[i] = (char *)malloc(6)` with `table[i] = malloc(6)` The reason is simple. If you cast right, you're safe, but if you cast wrong you're in trouble. Since it's safe to not cast there's no reason at all to cast. Read more about it here: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – klutt Jul 06 '17 at 10:05
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. when using the '%s" input format specifier, always include a MAX CHARACTERS modifier that is one less than the length of the input buffer so the data cannot overflow the input buffer. Such overflow is undefined behavior and can lead to a seg fault event. Suggest: `if( 1 != scanf("%5s", table[i]) ) { fprintf( stderr, "scanf for string failed" ); // pass all malloc'd pointers to free then exit( EXIT_FAILURE ); }` – user3629249 Jul 06 '17 at 17:33

2 Answers2

4

The problem is this:

scanf("%s", &table[i]);

and this:

printf("Person nr %d : %s ", i+1, &table[i]);

You seem to forget that table[i] is a pointer to char which is what a string basically is. By using &table[i] you get a pointer to the pointer, which is of type char **. You basically treat the pointer itself as a string, not the memory it points to.

Simply drop the address-of operator and you can read strings up to five characters (plus the terminator).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thats it. Thank you very much, I thought it was a stupid mistake, but I could not find it myself. Have a nice day! – Leonek Jul 06 '17 at 08:51
0

&table[i] is of the type char** and table[i] is the type char*. table[i] is the pointer for your string. You should change the &table[i] to table[i].

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Shaw Song
  • 1
  • 1