I am trying to pass an array of pointers to strings (names) into a function (foo) and read from it. The below code produces a segmentation fault. Can someone please help me figure out why this code is resulting in a segmentation fault? I want to be able to pass the array names[][] through a function and work with the data like I would if I were using names[][] outside the function.
void foo(char *bar[]) {
printf("%s\n", bar[0]);
}
//---------------Main-------------
char args[][50] = {"quick", "brown", "10", "brown", "jumps", "5"};
int i = 0;
int numbOfPoints = (sizeof(args)/sizeof(args[0]))/3;
//array of all the locations. the number will be its ID (the number spot in the array)
//the contents will be
char names[numbOfPoints][100];
for(i = 0; i < numbOfPoints; i++) {
char *leadNode = args[i*3];
char *endNode = args[i*3 + 1];
char *length = args[i*3 + 2];
int a = stringToInt(length);
//add name
strcpy(names[i],leadNode);
}
//printing all the names out
for(i = 0; i < numbOfPoints; i++) {
printf("%s\n", names[i]);
}
foo(names);
names[i]is not compatible withchar* []. You should changefoo(char *bar[])tofoo(char *bar).namesis not an array of pointers.