Arthur Penguin
Arthur Penguin

Reputation: 83

Incompatible pointer type when passing a pointer of pointer

I'm trying to pass a pointer of pointer in order to modify its content in another function. In order to do this, I've read that I need to use a pointer of pointer of pointer so here's my "code" so far:

void modify(s8 ***strings){
    // Modifies the strings
}

void main(){
    s8 strings[9][95];
    for(i = 0 ; i < 9 ; i++){
        for(j = 0 ; i < 95 ; i++){
            strings[i][j] = 0;
        }
    }
    modify(&strings);
}

s8 is a special type that is used by the MicroBlaze I'm using, is this a problem? I've also read that I need to use const but I don't understand where and why?

What about the size? Will I need to use a malloc inside modify in order to reserve a space in the memory? Or is it possible to just set the modify function as:

void modify(s8 *strings[9][95])

Right now, my issue is the following: I get a warning on the last line (modify(&strings))about the pointer type that is incorrect.

passing argument 1 of 'modify' from incompatible pointer type

Thanks for your answers!

Upvotes: 0

Views: 261

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409364

First of all an array of arrays is not the same as a pointer to a pointer.

Secondly, if you jut want to modify the strings in the array you don't need to emulate pass by reference by passing a pointer to strings. All you need is to pass it as a normal argument, and of course modify the function suitably:

void modify(s8 strings[][95]){
    ...
}

int main(void){
    s8 strings[9][95];
    ...
    modify(strings);
    ...
}

In the modify function, you declare strings to be a pointer to an array of 95 characters. Then when you pass strings from the main function it decays to a pointer to its first element, which happens to be a pointer to an array of 95 characters.

If you wanted to be more explicit about the strings argument in modify being a pointer, you could do e.g.

void modify(s8 (*strings)[95]){
    ...
}

Upvotes: 5

Related Questions