How can you interpret the following line of code?
int (*arrayABC)[10];
In my textbook, it says that we have a pointer to a pointer to the 0th element of an integer array.
However, I don't quite understand this.
My interpretation: We have some variable, which gets as its value some address. This address is then the address of the 0th element of an UNNAMED integer array. Basically we have a pointer to the 0th element.
Why then to have a pointer TO A POINTER?
This is a pointer to an array. It is not a pointer to a pointer. Arrays and pointers are different. An array has an address, but an array is not an address. An array is a series of contiguous elements.
This pointer points to the whole array and not just the first element, in the same way that a float * points to the whole float and not just the first byte.
If you have for example:
int foo[10];
int (*arrayABC)[10] = &foo;
then the expressions (*arrayABC) and foo are identical. E.g. foo[3] is the same as (*arrayABC)[3].
If you have an object of a type T then a pointer to the object is declared like
T obj;
T *ptr = &obj;
Now let's the type T is defined the following way
typedef int T[10];
Thus the code above
T obj;
T *ptr = &obj;
can be rewritten using the typedef definition like
int obj[10];
int (*ptr)[10] = &obj;
In the both cases, when T is some abstract type and when T is an alias for int[10], ptr is a pointer to an object. In the last case ptr is a pointer to an array of 10 elements of type int. That is the object pointed to by ptr has array type. Arrays and poiters are different types.
Try the following simple demonstrative program
#include <stdio.h>
int main( void )
{
typedef int T[10];
T a;
T *pa = &a;
printf( "%zu\n", sizeof( *pa ) );
int b[10];
int ( *pb )[10] = &b;
printf( "%zu\n", sizeof( *pb ) );
}
Its output is
40
40
As you can see the both values are equal to the size of an integer array of 10 elements. So the pointers point to arrays.
If you write
int *arrayABC[10];
you will get an array of 10 pointers of type int *.
If you write
int (*arrayABC)[10];
you will get a pointer to an array of 10 integers.
In my textbook, it says that we have a pointer to a pointer to the 0th element of an integer array.
Your textbook is ... badly worded, let's leave it at that. It's a pointer to a 10-element array of int.
I can kind of see where the book is coming from. Given the declaration
int (*a)[10];
the expression *a will have type int [10] (10-element array of int). However, unless that expression is the operand of the sizeof or unary & operators, it will be converted ("decay") from type int [10] to int *, and the value of the expression will be the address of the first element of the array:
Expression Type "Decays" to Value
---------- ---- ----------- -----
*a int [10] int * Address of first element of the array;
equivalent to `&(*a)[0]`.
Thus, if I call a function like
foo( *a );
what the function actually receives will be a pointer to the first element of the pointed-to array (its type will be int *, not int **).
So in effect, yes, a often winds up behaving as a pointer to a pointer to the first element of the array; however, its type is not "pointer to pointer to int", and type matters. Given:
int (*a)[10];
int **b;
then you'll get different behavior for the following:
sizeof *a vs. sizeof *b
a + i vs b + i (also applies to a++ vs b++)
Related
Before anyone closes this question because there's another related one, hear me out. I've already looked at the other question and I didn't get it. I would like to create a pointer to a multi dimensional array but I don't know how. I thought I was supposed to do it like this:
int test_arr[2][4];
int *ptr_array = test_arr;
But when I do that, I get a warning saying:
incompatible pointer types initializing 'int *' with an expression of type 'int [2][4]'
I have no idea what I'm doing wrong. Can someone help me please?
In order to do pointer arithmetic the pointer has to know the size of what it is pointing to.
int test_arr[2][4]; is equivalent to 2 elements of type int[4]. So whenever you add 1 to the pointer, it will jump the size of 4 integers. If you had int* it would increment the size of a single integer only.
Like said in the comments, what you want is: int (*ptr_array)[4] = test_arr;
I know, the syntax is a little weird, but that's how you do it.
You need to understand the basics.
[Below in post, read 1D as 1 dimension and 2D as 2 dimension]
First, lets take an example of pointer to 1D array:
Take an int type and a pointer pointing to it:
int x = 1;
int *px = &x;
The type of x is int and the type of &x is int * and type of px is int * i.e. it can hold the address of an int type variable. Hence the assignment is valid.
Take a 1D int array and a pointer pointing to it:
int arr[2] = {1,2};
int *parr = arr;
Note that the type of px and parr is same i.e. int * and assigning arr to parr is completely valid although the arr is a 1D array.
This is because of the fact that, in C, an array of type converted to pointer of type that points to the initial element of the array object but there are few exceptions to this rule and one of them is when used unary & operator.
The type of arr is int [2] but in the above statment (parr declare and initialisation statement) it is converted to pointer pointing to initial element of array which is an integer i.e. arr is converted to int * type in the int *parr = arr; statement. Hence, it is valid to assign arr to parr or to any variable of type int *.
As, I have mentioned that there are few exceptions to the rule (array of type converted to pointer of type....) and one of the exception is when used unary & operator. So type of &arr will be int (*)[2]. That means you can assign &arr to any variable of type int (*)[2].
int (*parr2)[2] = &arr;
i.e. parr2 is a pointer to an array of 2 integer.
The difference between this
int *parr = arr;
and
int (*parr2)[2] = &arr;
is that parr can hold address of any int pointer but parr2 can hold the address of any array of 2 integers. Both are pointing to same address but their types are different.
If you understood the above mentioned things completely then read below otherwise read the above part again.
Now lets come to 2D array.
Take a 2D array and a pointer pointing to it:
int arr[2][2];
int (*ptr)[2] = arr;
In the above ptr declare and initialisation statement, the type of arr is int (*)[2]. Why??
Recall the rule mentioned above - array of type converted to pointer of type that points to the initial element of the array object.
arr is array of 2 1D arrays of size 2 (2 int elements). So, it's first element is array of 2 int and, by rule, it will be converted to pointer to the initial element i.e. pointer to
1D array of 2 int which is int (*)[2].
Hence, you can assign arr to a pointer variable of type int (*)[2].
The type of &arr is int (*)[2][2], so this is also perfectly fine:
int (*ptr2)[2][2] = &arr;
So, there are two ways you can have pointer to 2D arr but remember that the types of ptr and ptr2 is different. This matters, for e.g., if you add 1 to pointer ptr it will be incremented by size of int [2] but if you add 1 to ptr2 it will be incremented by size of int [2][2].
How to access the array members using pointer:
In case of 1D array:
int arr[2] = {1,2};
int *parr = arr;
int (*parr2)[2] = &arr;
// access arr members via parr pointer
printf ("%d\n", parr[0]); // this will give value of first member of array arr
// access arr members via parr2 pointer
printf ("%d\n", (*parr2)[0]); // this will give value of first member of array arr
Similarly, in case of 2D array:
int test_arr[2][4];
int (*ptr_array)[4] = test_arr;
int (*ptr_array2)[2][4] = &test_arr;
// access test_arr members via ptr_array pointer
printf ("%d\n", ptr_array[0][0]); // this will give value of first member of array test_arr
// access test_arr members via ptr_array2 pointer
printf ("%d\n", (*ptr_array2)[0][0]); // this will give value of first member of array test_arr
Based on above explanation, try yourself creating multidimensional array (> 2D array) pointers and play with them.
Hope, above explanation resolves your all confusions related to pointer to a 2D array and you understood now why compiler is giving incompatible pointer types .... warning on statement int *ptr_array = test_arr;. It's all about type compatibility during initialisation/assignment.
I couldn't understand use of (int*) p in following program for pointer to an array
#include<stdio.h>
void main()
{
int s[4][2];
int (*p)[2];
int i,j,*pint;
for(i=0;i<=3;i++)
{
p=&s[i];
pint=(int*)p; /*here*/
printf("\n");
for(j=0;j<=1;j++)
printf("%d",*(pint+j));
}
}
can i use *p instead of (int*) p here. thanks in advance
In your code,
pint=(int*)p;
p is of type int (*)[2], i.e., pointer to an array of 2 ints, and pint is a int *. They are not compatible types, so you need the cast.
If you have an array declaration like this
int s[4][2];
then these three pointers have the same value
int ( *p1 )[4][2] = &s;
int ( *p2 )[2] = &s[0];
int *p3 = &s[0][0];
because all the pointers point to the initial address of the extent of memory allocated for the array.
However the pointers have different types because they point to objects of different types.
The first pointer points to the array in whole as a single object.
the second pointer points to an array element that in turn has the array type int[2].
And the third array point to a scalar object of the type int.
So you may not assign directly one pointer to another because they are incompatible though as it was mentioned they have the same value (address).
You need to cast one pointer type to another pointer type explicitly.
This assignment in the original program
p=&s[i];
assigns the address of each element (array of the type int[2]) to the pointer. In fact it is the address of the first element of the array that is it is equal to &s[i][0]. However the first expression and the last expression have different pointer types.
So you need to use casting in this assignment
pint=(int*)p;
Now using the pointer arithmetic in this expression
*(pint+j)
you can traverse all scalar elements of the array initially pointed to by the pointer p.
Pay attention to that this declaration of main
void main()
is nit a standard declaration.
You should declare the function main like
int main( void )
I am studying C on my own and I am very confused on why
int b[2][3];
int (*p)[3] = b;
is different from
int b[2][3];
int *p[3] = b;
Can anyone explain the difference?
One is a pointer to an array, while the other is an array of pointers.
This:
int (*p)[3]
Is a pointer to an array of size 3 of int. This means *p gives you the array and (*p)[1] gets the element at index 1 of that array.
It is also allowed to assign b to this pointer. This is because b is an array of size 2 whose members are an array of size 3 of int, and arrays decay to a pointer to their first element in an expression, so b decays from type int[2][3] to type int(*)[3] which matches p. So you can also for example use p[1][2] to access the same array element as b[1][2].
In contrast, this:
int *p[3]
Is an array of size 3 whose members are int *. Attempting to assign b to this won't work because you can't assign to an array directly and because the types are not compatible.
You could however do this:
int x[5], y[4], y[6];
p[0] = x;
p[1] = y;
p[2] = z;
I know, that static arrays are laid out contiguously in memory.
So, for example, int T[10][10] is basically stored the same way as int T[100].
I can access element of index i, j many ways, for example:
int T[10][10];
/*filling array*/
int i=3, j=7;
int x = T[i][j];
//EDIT - old and wrong: int * ptr = T;
int * ptr = &T[0][0];
int y = *(ptr + 10* i + j);
On the other hand, when I create dynamicaly allocated 2-dimensional array by myself:
int ** T;
T = malloc(10 * sizeof(int *));
for(i = 0; i < N; i++)
T[i] = malloc(10 * sizeof(int));
My array contains pointers to
It is obvious, that I can access element of this array by:
int i=3, j=7;
int x = *(*(T+i)+j);
And now my question: why and how does it work for static arrays?
Why does
int T[10][10];
/*filling array*/
int i=3, j=7;
int x = *(*(T+i)+j);
return good value to x, when this table doesn't contain pointers to arrays? *(*(T+i)) shouldn't have sense there in my opinion, end even if, it should return T[0][i], as T points at the first element of array. How does compiler interpret this, is * something other than dereference here? Enlighten me.
For starters:
int * ptr = T;
That's not going to actually work, at least without your compiler yelling at you. Very loudly. The correct way to do this is:
int * ptr = &t[0][0];
This point is actually very relevant to your question.
As you know, when used in an expression, an array gets decayed to a pointer. For example:
char foo[10];
bar(foo);
When used in an expression, like a parameter to a function, the array decays to a pointer to the underlying type. foo gets you a char *, here.
However, and this is the key point: the array only decays one level. If the array is a two-dimensional array, the array does not get decayed to the underlying value, an int in this case. The two dimensional array reference decays to a pointer to a one-dimensional array:
int T[10][10];
/*filling array*/
int i=3, j=7;
int x = *(*(T+i)+j);
The sequence of steps that occurs here:
T decays to a pointer to an array of 10 integers, or int (*)[10]
The addition of i advances the pointer by the given value. The pointer is advanced by the size of the element being pointed to. Since the pointer points to array of 10 integers, the pointer is advanced accordingly. If i was 2, the pointer is advanced by "two arrays of 10 integers", loosely speaking.
The * operator takes a "pointer to an array of 10 integers" and gives you "an array of 10 integers" as a result. In other words: from int (*)[10] to int [10].
Since the result is used in an expression, namely the left operand of + j, and the left operand is an array type, the array type decays to a "pointer to int".
j is added to the result, and dereferenced.
Why does
int T[10][10];
/*filling array*/
int i=3, j=7;
int x = *(*(T+i)+j);
return good value to x
The magic is all in *(*(T+3)+7) (I have converted to the literal values).
T is an array (of size 10) of arrays (of size 10) of int.
When T is used in an an expression it decays into a pointer to its first element, so it decays to "pointer to arrays (of size 10) of int".
Adding an integer to that pointer will advance to the fourth element of the array.
So T+3 is a pointer to an array of 10 ints, and specifically the fourth such array in T.
*(T+3) indirects through that pointer to give an l-value of type "array of 10 ints".
Ah-ha! That is another array being used in an expression - so it decays into a pointer to it's first element! (It wouldn't decay in sizeof, so sizeof(*(T+3)) would be typically 40.)
(*(T+3) + 7) just points at the eight element in the array, and ...
*(*(T+3) + 7) is an l-value of type int!
The following snippet declares a 2-D array of 4 X 10 using malloc function
/* Declare a pointer to an array that has 10
ints in each row. */
int (*p)[10];
register int i, j;
/* allocate memory to hold a 4 x 10 array */
p = malloc(40*sizeof(int));
But I do not understand how does p become a 2-D array. Initially p is declared to be an array of pointers that point to int. What happens after the call to malloc ? I am unable understand this.
In C, pointers and arrays are not the same, despite looking very similar. Here p is of type "pointer to array of 10 ints". You're using it as a "pointer to array of 4 arrays of 10 ints", which is a single block of memory (the only pointer is the outermost pointer). It's basically a dynamically allocated int[4][10].
The trick to reading these definitions is to realize that they're written the same way you use the item. If you have:
*x[10];
The array subscript is applied first, then the pointer dereference. So it's an array of pointers if you define int *x[10]. If you use parenthesis to override normal precedence, you can get the pointer dereference to happen first, so you have a pointer to an array.
Confusing? It gets worse. In function arguments, the outermost array of a function parameter is converted into a pointer.
int *p[10]; // array of 10 pointer to int
int (*p)[10]; // pointer to array of 10 ints
void foo(int *p[10] /* pointer to pointer to int */);
void foo(int (*p)[10] /* pointer to array of 10 ints */);
Further, arrays are converted to pointers when you use them.
int x[10]; // array of 10 int
sizeof(x); // 10 * sizeof(int)
int *y = x; // implicitly converts to a pointer to &x[0]!
sizeof(y); // sizeof(int *)
This means that you can allocate memory for an array of arrays, then let that implicitly convert to a pointer to an array, which you in turn use as if it were an array of arrays!
Anyway, this is all very confusing so please do not ever use this in production code - at least, not without a clarifying typedef:
typedef int vector[3];
vector *array_of_vectors; // actually a pointer to a vector,
// but you can use it as an aray to a vector if enough
// memory is allocated
The memory, worth 40 ints, is reserved to the pointer p. p points at his memory block. It so happens that p chooses to organize this memory as 10 equal parts, each of which happen to hold 4 ints' worth.
That's if this code is actually correct. My C is very rusty at this point.
First, some background information:
Except when it is the operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" is converted ("decays") to an expression of type "pointer to T", and its value is the address of the first element in the array. For example, given the array
int a[10];
anytime the expression a appears in the code, its type will be converted from "10-element array of int" to "pointer to int", or int *, except for cases like sizeof a, _Alignof a, and &a. If we have a 2D array of T, such as
int a[10][10];
the expression a will be converted from type "10-element array of 10-element array of int" to "pointer to 10-element array of int", or int (*)[10] (look familiar? that's the type of your pointer p).
If we want to dynamically allocate an N-element array of type T, we write something like
T *p = malloc(N * sizeof *p);
sizeof *p is equivalent to sizeof (T). In this particular case, type T is "10-element array of int", or int [10]. We want to allocate 4 such arrays, so we can write
int (*p)[10];
p = malloc(4 * sizeof *p);
This allocates space for 4 10-element arrays of int, and assigns the result to p. (sizeof *p == sizeof (int [10])).
So how does this become a 2D array?
Remember that the expression a[i] is equivalent to *(a + i); we find the address of the i'th element of type T following a and dereference the result. In this case p[i] gives us the address of the ith 10-element array of int following p. Since we dereference the pointer as part of the subscript operation, the type of the expression p[i] is "10-element array of int". Thus we can subscript this expression again and get p[i][j].