0

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!

1 Answers1

5

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]){
    ...
}
Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for your answer, passing the array as a normal argument is what I've done first but then my issue is that the last element (strings[7]) isn't what I set in the modify function once I get back into the main function. That's why I thought I was trying to do it wrong. Do you have any idea why? Meanwhile I'll try your syntax in order to see if I still have this issue! – Arthur Penguin Jun 21 '16 at 08:00
  • 1
    @ArthurPenguin Sounds like you have a bug inside the function then, which is perhaps not related to the parameter passing. – Lundin Jun 21 '16 at 08:02
  • 1
    Alternatively, use VLAs: `void modify(size_t x, size_t y, s8 strings[x][y])`. – Lundin Jun 21 '16 at 08:14
  • @Joachim Pileborg Finally had time to test your proposition, the issue was indeed coming from a misunderstanding between pointer of pointer and 2 dimensional array! Thanks :) – Arthur Penguin Jun 22 '16 at 13:33