When creating a pointer array in c what does the effect of adding parentheses do?
For example
int (*poi)[2];
vs
int *poi[2];
C. In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays.
Uses of pointers:To pass arguments by reference. For accessing array elements. To return multiple values. Dynamic memory allocation.
In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same. There are a few cases where array names don't decay to pointers.
Following is the declaration for array of pointers − datatype *pointername [size]; For example, int *p[5]; It represents an array of pointers that can hold 5 integer element addresses.
Pointer to an array of 2 int
s:
int (*poi)[2];
An array of two int
pointers:
int *poi[2];
Normally Array has higher precedence than the pointer, but if you add the parentheses then the pointer comes "first".
The index operator []
binds stronger than the derefentiation operator *
.
int *poi[2]
translates to:
If you see poi, apply [x]
to it, then dereference the result via *
and you get an int
.
So it's an array of 2 pointers to int.
In
int (*poi)[2]
the parantheses force the *
to be applied first.
So anytime poi is used, if you apply *
first, and then [x]
you get an int
.
So it's a pointer to an array of 2 int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With