I have a very confusing question to ask and it is the difference between array of pointers & pointer to an array. I want to understand this concept very well so please please do help me understand this concept. I'll start with what I have understood about array of pointers. An array of pointers is possible beacuse pointers are similiar to variables. *pt[3] is an array of 3 pointers i.e 3 locations are set aside for the storage of the addresses of 3 variables. pt[0] corresponds to the address of the 1st element and *pt[0] gives us the value of the 1st element. Now when we utilize array of pointers for 2-D arrays, the addresses stored in the array are starting addresses of 1-D arrays and hence we can access the entire 2-D array using the array of pointer construct i.e. if the address of the 1-D array is stored in pt[2] then the location of the 1st element of the 3rd array can be written as: *(*(pt+2)+0), hence all the elements of all the arrays an be accessed in this manner by proper indexing. Another added advantage is that we can have variable length arrays. Am I right regarding this? When the concept of pointers to array comes all hell breaks loose. Pointers to arrays are declared as (*ptr)[4]. I don't understand what is the difference between this definition and the one I gave above. I don't understand what is stored in the array of (*ptr)[4]? Is it addresses of arrays?? If it is then how is it any better than array of pointers?

Recommended Answers

All 2 Replies

An array of pointers is possible beacuse pointers are similiar to variables.

Pointers are variables, and the values they hold are addresses.

*pt[3] is an array of 3 pointers i.e 3 locations are set aside for the storage of the addresses of 3 variables.

In the context of a declaration, yes. *p[3] is also a valid expression that dereferences the 4th element of an array.

Another added advantage is that we can have variable length arrays.

"Variable length" isn't the term you want, since it has connotations that don't apply (such as being able to grow and shrink the size of an array, or define the size at runtime). I think what you meant was a jagged array, where each sub-array can be of a different size. However, this only applies when taking into account that pointers can be used to simulate arrays. An actual 2D array cannot be defined as a jagged array because there's no way to tell the compiler that you want varying sizes for individual elements.

Pointers to arrays are declared as (*ptr)[4]. I don't understand what is the difference between this definition and the one I gave above.

The first is an array that contains pointers. The other is a single pointer that can point to an array type. It might help if you incorporated the concept of pointers with the concept of types. A pointer is a variable that holds an address, nothing more. There are many different types of pointers though, and they each hold the address of an object that itself has a corresponding type. So int* can only point to objects of type int. Likewise, int(*)[4] can only point to an array of 4 int. And int** can only point to objects of type int*, which in turn can only point to objects of type int.

Due to C's awkward declaration syntax, the parentheses are the only difference between int *p[4] and int (*p)[4], but that difference is very significant. You read it from the inside out (using excessive parens to highlight where that happens and what the difference is. Not legal C syntax):

  • (int (*((p)[4]))): p is an array of 4 pointers to int
  • (int ((*(p))[4])): p is a pointer to an array of 4 int

Is it addresses of arrays?? If it is then how is it any better than array of pointers?

Technically you could say that int (*p)[4] is equivalent to int p[1][4]. However, much like you don't say int p[1] when what you really meant was int *p, a pointer to an array has its place. Pointers to arrays are rarely used explicitly, but you'll see them fairly often in error messages because the first dimention of an array decays into a pointer when passed to a function.

Thanks for replying and clearing the confusion...!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.