I'm trying to play around with c and pointers since I'm new to the language but I'm not sure why I'm getting a segmentation fault, when I have initialized everything to null then going to re-write the array of char pointers? Can someone explain what I did wrong?
#define MAX_SIZE 70
void gridTest(char*[]);
int main()
{
char *grid[MAX_SIZE] = {0};
testGrid(grid);
return 0;
}
void testGrid(char *grid[]){
for(int i=0;i<MAX_SIZE;i++){
*grid[i] = ' ';
}
for(int j=0;j<MAX_SIZE;j++){
printf("The Character is space, test: %c",*grid[j],j);
}
}
ERROR
Segmentation fault: 11
*grid[i] = ' 'which dereference those null pointers, leading to undefined behavior.charanyway, why an array of pointers tochar? Why not a plain array ofchar?main()initialises all the elements ofgridto be null pointers.testGrid()modifies data pointed to by those elements. Doing that on null pointers is undefined behaviour.*grid[i] = ' 'does not magically convertgrid[i]from a null pointer to something that can be written to.