I need to store a pointer to a char array in a struct, and then modify/access the arrays content. How can I do that?
I can only think of something similar to this, but I don’t get to the complete compilable solution.
struct foo {
unsigned char *array;
};
And then:
unsigned char array[512];
struct foo *foo;
foo->array = array;
In another function which receivers pointer to struct:
*(foo->array[0]) = 'K';
Your code is almost fine:
The problem with your code
*(foo->array[0])is that you try to dereference acharwhich is not even a pointer.You also need to allocate memory for the struct – currently
foopoints to some random memory location where an access will most likely crash your program: