0

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
4
  • You are not allocating memory for each pointer inside the array. Commented Sep 30, 2018 at 15:31
  • You have an array of null pointers. Then you do *grid[i] = ' ' which dereference those null pointers, leading to undefined behavior. Commented Sep 30, 2018 at 15:31
  • And if you're just need space for a single char anyway, why an array of pointers to char? Why not a plain array of char? Commented Sep 30, 2018 at 15:32
  • main() initialises all the elements of grid to be null pointers. testGrid() modifies data pointed to by those elements. Doing that on null pointers is undefined behaviour. *grid[i] = ' ' does not magically convert grid[i] from a null pointer to something that can be written to. Commented Sep 30, 2018 at 15:40

2 Answers 2

2

Check this line

  *grid[i] = ' ';

you are trying to store the space character in a memory pointed to by grid[i], that's cool, but where does that one point to?

Answer is: Invalid memory (a null pointer, it is). The memory address you're trying to use is invalid and attempt to deference it invokes undefined behavior.

That being said, seeing your usage, you don't need an array of char pointers, an array of char would suffice. Change your array definition to

 char grid[MAX_SIZE] = {0};

and change the called function as

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);
        }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

but now in this case, I'm not actually passing the array, I'm making a copy of it, thats why I was trying to use pointer, so I can pass it by reference
@decentProgrammer why?
so that when I later modify the program to make a simple text game, I won't have to deal with a "copy" of the data and not the actual data.
@decentProgrammer Don't forget that arrays naturally decays to pointers to their first element. When you have an argument like char grid[] it's really the same as char *grid. I.e. all that is passed is the pointer, not the whole array.
1

By declaring an array of char pointers, you become responsible for making those pointers point to something. If you just want to get a 70x70 grid, you can get around that by declaring grid as an array of char arrays:

char grid[MAX_SIZE][MAX_SIZE];

That way, the memory is automatically allocated and ready for use.

*grid[i] is equivalent to grid[i][0], so testGrid currently accesses only the first column in each row. To access all cells in the grid, you could use two nested for loops and access them with grid[i][j] or similar.

3 Comments

then, If i don't want to use a pointer, how is it possible to return the char array that I passed?
The array acts as a pointer in many ways. In particular, you can pass the array as a pointer to a function, as you did with testGrid, and that function can still operate on the pointer as if it were an array. The difference is that with a pointer, you can change what it points to, while an array always "points" to its data.
Oooooooooooooooooh Wow, I never knew this. that solves my issue then. I just tested it. Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.