Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to Array in C

Tags:

arrays

c

pointers

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?

like image 638
Kevin Wu Avatar asked Jan 04 '17 21:01

Kevin Wu


People also ask

What is pointer to an array in C?

When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory. Pointers to an array points the address of memory block of an array variable.

Can We Make pointer array in C?

When we require multiple pointers in a C program, we create an array of multiple pointers and use it in the program. We do the same for all the other data types in the C program.

Can a pointer point to an array?

Pointer to an array points to an array, so on dereferencing it, we should get the array, and the name of array denotes the base address. So whenever a pointer to an array is dereferenced, we get the base address of the array to which it points.

What does it mean arr [- 1?

-1 means the last element, -2 the second last, and so on.


1 Answers

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].

like image 71
M.M Avatar answered Sep 19 '22 20:09

M.M