Search code examples
carraysmemory-managementc-strings

How to pass a 1D array of 500 pointers to a function in C


The programm is compiling ok but once the user inputs something to the array it crashes. Any help will be appreciated :)

#include <stdio.h>
#include <stdlib.h>

void Register(char *arr[],char arr2[]);
int main() {
    char *Username[500];
    char table1[20];
    Register(Username,table1);
    return 0;
}   
void Register(char *Username[],char table1[]){
    int i;
    for(i = 0; i < 500; i++){
        scanf("%s",&table1);
        Username[i] = table1;
        printf("for i = %d username[%d] is %s\n\n",i,i,Username[i]);
    }
}

Solution

  • The problem is that when you pass your arrays they decay to pointers to their first element. For "array" arguments something like e.g. char table1[] is actually char *table1. And this is a problem when you try to use the address-of operator & in scanf.

    When you use &table1 in the scanf call you get a pointer to the pointer, which has the type char **, not the expected char *. This mismatch between the format %s and the expected type leads to undefined behavior and probably your crash.

    The solution to this crash is to never use the address-of operator for reading strings (with e.g. the %s format), as it's even wrong when you have an actual array:

    scanf("%s",table1);
    

    As for the problem of making all elements of Username point to the single string in table1, I recommend that you use arrays of arrays for Username instead:

    char Username[500][20];
    

    This array decays to a pointer to an array, with the type of char (*)[20], which needs to be part of the Register function declaration:

    void Register(char (*Username)[20]);
    

    Then you can use this directly in the call to scanf:

    scanf("%19s", Username[i]);
    

    Also note how I limited the length of the input string, so you can't read more than the arrays can handle (and it's 19 because the array need to fit the string null-terminator as well).