tags:

views:

314

answers:

9

I can declare:

int (*ap)[N];

So ap is pointer to int array of size N. Why is this ever useful? If I pass it to function, what useful thing it can do with it that it could not do with just a normal pointer to the array's contents?

C FAQ say:

2.12: How do I declare a pointer to an array?

Usually, you don't want to.

+2  A: 

It's not useful, really. But sometimes pointers to arrays are used e.g. in Microsoft Windows API - I've seen a lot of this stuff there.

Adam Woś
what for used in the win API ?
zaharpopov
You can google for '"pointer to array" inurl:msdn.microsoft.com'. Examples: http://msdn.microsoft.com/en-us/library/ms221359.aspx, http://msdn.microsoft.com/en-us/library/ms714816%28VS.85%29.aspx etc.
Adam Woś
@adam.wos: looked at these links - this doen'st look like real pointers to arrays in C, just pointers
zaharpopov
You're right in the strictest term. For them, "pointer to array" is a pointer to the first element, not a pointer to array as you said in the question.
Adam Woś
A: 

You never need to do this since the array name itself is a pointer to the beginning of the array

ennuikiller
Not never. Se my answer - in cases where the placement of the array could be moved by the OS then you need to dereference twice. But I agree it's *very* esoteric.
Cruachan
+5  A: 

If you increment the pointer, it will then point to the start of the next group of N elements.

This is not a big deal and it's use is up to the developer.

Steve Emmerson
Gosh, have you even seen this used?
bmargulies
Great to see someone knows that the two aren't the same =)
Stephen Canon
@bmargulies: I've used it for an array of MD5 checksums where each checksum is 16 bytes.
Steve Emmerson
A: 

This will probably careen off into subjective/argumentative, but...

In my opinion, pointers to arrays are in the language because they fell into the language. There are pointers to every other declarable data type, so these are here too. I've never seen anyone get really useful work out of them. Hypothetically, they allow a prototype that demands an array and not a pointer to the element, but ...

bmargulies
...but they're the same thing (a pointer to the (first) element and a pointer to an array)? :)
Adam Woś
They evaluate to the same location, but the types are different, and types matter.
John Bode
A: 

It seems pretty useless to me to do a pointer to an array. In C, an array is already a pointer to a block of that data type.

int (*ap)[N];
int **ipp;

are both the same data type (a pointer to a pointer to an Integer). The only difference there is that there is space for N integers allotted for ap.

There's no need to pass an array by a pointer to a function, like, for the purpose of changing the contents of the array within that function because it's already a pointer. As a general rule, I'd say it's unnecessary and just creates an additional need to dereference the pointer to get at the data in the array. But, I'm sure there's a program or algorithm somewhere that could find a legitimate use for it.

chaosTechnician
No, they are not the same data type. So you're saying that it's okay to do `ipp = ap;` and `ap = ipp;` (assuming that the pointers are pointing to somewhere useful)?
Alok
They are not the same. Try `++ap` and `++ipp` and see what you get.
Stephen Canon
Arrays **are not** pointers. An array *expression* may be implicitly converted to pointer type in most contexts, but that's not the same thing. In your example, `ap` and `ipp` are not compatible types; you could not assign the value of `ap` to `ipp` or vice versa (at least not without an explicit cast).
John Bode
+2  A: 

There are situations where you want to pass the memory location across programs. e.g a windows API might expect you to pass a pointer to data structure or an array where as you are programming in some other language, say c#. Windows API does not care how the target language handles array's, for windows API it is just a stream of bytes in memory and it will fill it, send it back to you. to avoid cross language type missmatch in some cases we use pointer to array rather than implicit array name as pointer. More over it is not guaranteed that the implicit array name is a long pointer, some compilers might optimize it to be a relative value withing segment. a pointer to an array guarantees that it is of the order of machine register size and you can point to a location anywhere in the available RAM.

scienty
can you show code example of this used?
zaharpopov
+7  A: 

A pointer to an array can be used to dynamically allocate a multi-dimensional array N, where N-1 dimensions are known. Below creates a Nx3 array.

int (*ap)[3];
ap = malloc(N * sizeof(*ap));
/* can now access ap[0][0] - ap[N-1][2] */

@Adam E/Cruachan, This is not the same thing as a pointer to a pointer. ap is a single pointer to a block of memory containing three consecutive integers. ap++ will advance the pointer address to the next block of three integers. for int **pp;, pp points to an integer pointer, each of which can point to an integer anywhere in memory.

         +-----+                +------+    +-----+
 ap ---> | int |   vs.  pp ---> | int* | -> | int |
         | int |                +------+    +-----+
         | int |        pp+1 -> | int* | -\
         +-----+                +------+   \   +-----+
 ap+1 -> | int |                  :  :      -> | int |
         | int |                               +-----+
         | int |
         +-----+
           : :  
Mark Tolonen
but arguably ap is just a 2d array.
Pod
Yes, but it isn't the same as "int ap[4][3];". It allows the 2nd dimension to be dynamically allocated.
Mark Tolonen
Ah I see, in which case I admit my error and withdraw my answer.
Cruachan
+1  A: 
John Bode
A: 

I think the conclusion from this whole discussion is never. Nobody here really demonstrated a use for this construct where something else wouldn't work in a more comprehensible way.

zaharpopov