Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Pointer convoluted syntax

I was fidgeting with the pointers thingy.graduated from pointers -> array of pointers -> function pointers -> pointer to pointers..

Here's what i am stuck at...mostly the convoluted syntax.

Lets say i have an array of integers.

int arr[4] = {1,2,3..};

also i have array of pointers

int* ptr[4];
ptr[0] = arr; 

here ptr[0] will point to 1 and ptr[1] will point to some other location

This works perfectly !!

Now considering above background i tried this.

char* crr[4] ={"C","C++","C#","F#"};
char** btr[4];
btr[0] = crr;

which works as pointer in oth element of btr is pointing to another pointer element in crr.

Then i tried this.

char* crr[4] ={"C","C++","Java","VBA"};
char** btr[4]= &crr; // Exception: cannot convert char* [4] *  to char**[4]

but when i do this it works :O

char* crr[4] ={"C","C++","Java","VBA"};
char* (*btr)[4]= &crr;

i have not understood the last two scenarios. The use of brackets on RHS Pls explain.

like image 792
Haswell Avatar asked Dec 09 '25 23:12

Haswell


1 Answers

char** btr[4]= &crr; // Exception: cannot convert char* [4] *  to char**[4]

That's trying to initialise an array (of pointers to pointers) from a pointer, which you can't do. If you wanted to initialise the first element to point to crr (as in your first example), then you'd do

char** btr[4]= {crr};

The last example is declaring a pointer to an array (of pointers), and initialising it from a compatible pointer.

Note that your original array should be of const char *, not char *, since it contains pointers to (constant) string literals.

like image 120
Mike Seymour Avatar answered Dec 12 '25 12:12

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!