I'm reading a book on C and I stumbled upon the following piece of code with advanced pointer use:

Code:
```int main()
{
float (*a);
int n=3, i, j;

a = (float(*))malloc(n*sizeof(float));
for(i=0; i < n; i++)
for(j=0; j < 5; j++) a[i][j] = ...```
According to the book *a is an array of 5 float elements so a is a pointer to an array of 5 float elements. Also, the book says that the n rows are in a sequence. The last remark I can understand, because malloc allocates n*5 float elements. I just don't understand why a is a pointer to an array of 5 float elements. I would think it is an array to n*5 elements. Does anybody see what I'm overlooking here? 2. Originally Posted by mvanrijnbach
I just don't understand why a is a pointer to an array of 5 float elements. I would think it is an array to n*5 elements. Does anybody see what I'm overlooking here?
It just happens to be the syntax of how to declare a pointer to an array. Think about the book's point that *a is the array (although it does not exist until later), thus a is a pointer to an array.

By the way, you can simplify this:
Code:
`a = (float(*))malloc(n*sizeof(float));`
to:
Code:
`a = malloc(n * sizeof(*a));` 3. You are creating a two dimensional array. You know one dimension in your initialization (5). For every element of your original a you are allocating another n size array of which you return the address. So you are actually working with pointers to pointers. 4. 5. Originally Posted by boxden
You know one dimension in your initialization (5). For every element of your original a you are allocating another n size array of which you return the address.
Not quite. It is more like space is allocated for n arrays of 5 floats, and a points to the first of these n arrays. Originally Posted by boxden
So you are actually working with pointers to pointers.
No, pointer to an array, not pointer to a pointer. 6. Originally Posted by laserlight Not quite. It is more like space is allocated for n arrays of 5 floats, and a points to the first of these n arrays.

No, pointer to an array, not pointer to a pointer.
The name of an array is a pointer? 7. Originally Posted by boxden
The name of an array is a pointer?
Nope. In many contexts, an array is converted to a pointer to its first element. That would be so for a[i], but then it would not be correct to try and pass a where a float** is required. 8. Yes, I am not sure if this is what laserlight was referring to, but the subject of pointer declarations and dereferencing is often a criticized design choice in C.

A lot of beginners are confused by a similar notation that means two different things: when you define a pointer:

Code:
`int *p;`
;

The SYMBOL * just identifies the fact that p is a pointer to type int.

Code:
`*p = 5;`
Later on in the code if you do this, you are saying "write to the address pointed by p the value 5".

What helped me greatly when learning C was my teacher always asking me what a pointer is when I got confused. The answer was always : "A POINTER IS AN ADDRESS".

Code:
`float (*a);`
*a identifies that a is a pointer (because it is a definition).
(i.e. an address to a type float , so to an array of 5 floats, which are obviously stored consecutively in memory because that's what an array does). 9. Perhaps this will help. The data is stored exactly as if one had done this:
Code:
```typedef struct foo {
float bar;
};

struct foo *a;```
The only difference is that instead of having to write
Code:
`a[i].bar[j]`
you can omit 'bar' and just write
Code:
`a[i][j]`
As you can see, 'a' can point to some number of items that each contain 5 floats. You can also see from such a definition that this is not compatible with float** because there is no double-pointer involved.
Just to reiterate, the second dimension is not a pointer, it is the array itself, with a defined size. 10. Precisely. The book didn't mention pointer-to-pointers and (*a) is not an array of 5 pointers, which is what I thought at first. Popular pages Recent additions 