The project I am currently working on requires four dynamically created arrays of strings (each string no longer than 50 chars). So, I am trying to write a function that takes a pointer to a pointer and dynamically allocates memory for that variable.
This is as far as I've gotten:
void makeArray(char*** arr[], int n) {
int i;
*arr = malloc(n*sizeof(char*));
for (i = 0; i<n; i++) {
*arr[i] = malloc(sizeof(char)*50);
}
}
int main() {
char** test;
makeArray(&test,4);
return 0;
}
When I compile and run, I get this error:
main.c:16:13: warning: passing argument 1 of ‘makeArray’ from incompatible pointer type [-Wincompatible-pointer-types]
makeArray(&test,4);
^
main.c:4:6: note: expected ‘char ****’ but argument is of type ‘char ***’
void makeArray(char*** arr[], int n) {
When I use C Tutor, the function appears to successfully take in my test
array and allocate 4
pointer slots. Then it successfully allocates 50 chars to the 0
th test
slot. However, when the loop runs again, I get an error.
I've been stuck on this for two days now, so I welcome any suggestions the kind users of Stack Overflow may have!