C FAQs 6.12

P

Philip Potter

arnuld said:
I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html

but it doe snot tell why should I use "pointer to arrays" ?

The first sentences of the link you quote are:
"Usually, you don't want to. When people speak casually of a pointer to
an array, they usually mean a pointer to its first element."

I've never seen a pointer-to-array used. I can't tell you any reason why
you should use them. They presumably exist for completeness.

Philip
 
M

Martin

There are occasions when they're useful. Some time back I wrote a
"tokenize" function that took a pointer to a string and returned
a pointer to an array of pointers to token strings.

A bit later, I wrote another function that took a FILE pointer
and used tokenize() on each line of the text file and returned a
pointer to an array of pointers of the type returned by
tokenize().

I'm not sure where I'm seeing the 'pointer to array' as queried by the OP.
You're talking about pointers to arrays of pointers, similar to argv.
 
P

Philip Potter

Morris said:
I thought the OP's query had already been well-answered, and was
responding to Philip's comment that "pointer to array" might only
exist for sake of syntactical completeness.

My example actually resulted in a pointer to an array of pointers
to arrays of pointers to strings to indicate that the construct
was actually useful.

Sorry for the noise/confusion.

You seemed to be talking about pointers to the first element of an
array, not pointers to arrays proper. A char * can be a pointer to the
first element of a char array, but a char (*)[] is a pointer to a char
array. argv is a pointer to the first element of an array of pointers to
the first character of a string. But since that's so convoluted people
colloquially say argv is a pointer to an array of char. If we took this
literally, that would mean it was char (*argv)[] instead of char **argv.

Nevertheless Martin raised the good point about multidimensional arrays
using pointers-to-arrays, which very much proved me wrong :)

Philip
 
K

Keith Thompson

Martin said:
I'm not sure where I'm seeing the 'pointer to array' as queried by the
OP. You're talking about pointers to arrays of pointers, similar to
argv.

Strictly speaking, argv is not a pointer to arrays of pointers. It
doesn't point to an array; it points to the first element of an array.
It's an important distinction, since a pointer to an array and a
pointer to the first element of an array are two distinct thingsm,
both potentially useful.

Pointers to arrays (at least pointer values if not pointer objects)
show up in multidimensional arrays. For example:

int arr[10][20];
...
arr[x][y];

arr is of type array 10 of array 20 of int, which decays to pointer to
array 10 of int (there's your pointer to an array).

arr[x] is of type array 10 of int, which decays to pointer to int.

arr[x][y] is of type int.

In most cases, though, a pointer to the first element of an array
(along with the separately remembered length of the array) is more
useful and flexible than a pointer to the whole array. You need an
element pointer for indexing (which after all is what arrays are for),
and the whole-array pointer implicitly contains the array's length
(which must be constant) in its type.
 
K

Keith Thompson

Philip Potter said:
Nevertheless Martin raised the good point about multidimensional arrays
using pointers-to-arrays, which very much proved me wrong :)

Um, I thought that was me (unless Martin also mentioned it in another
message). No biggie.
 
K

Keith Thompson

Philip Potter said:
He beat you to it: <[email protected]>

Indeed, your message hadn't hit my server when I wrote mine above.

Ok, I concede. But I claim credit for going into more detail (unless
I got something wrong, in which case I'll deny the whole sordid affair
ever happened).
 
M

Martin

Strictly speaking, argv is not a pointer to arrays of pointers. It
doesn't point to an array; it points to the first element of an array.

Keith,

I understand what you mean, but K&R2 describes argv as "a pointer to an
array of character strings" (K&R2, p114). I don't think they meant "array
pointer" though - and neither did I!

It's an important distinction, since a pointer to an array and a
pointer to the first element of an array are two distinct thingsm,
both potentially useful.

Ys, I hope that other messages on this thread have emphasised that
important distinction.

int arr[10][20];
...
arr[x][y];

arr is of type array 10 of array 20 of int, which decays to pointer to
array 10 of int (there's your pointer to an array).

Whoops, I think you'll find 'arr' decays to "pointer to array of 20 ints."

arr[x] is of type array 10 of int, which decays to pointer to int.

arr[x] is of type array 20 of int, which decays to pointer to int,

arr[x][y] is of type int.
Indeed.


In most cases, though, a pointer to the first element of an array
(along with the separately remembered length of the array) is more
useful and flexible than a pointer to the whole array. You need an
element pointer for indexing (which after all is what arrays are for),
and the whole-array pointer implicitly contains the array's length
(which must be constant) in its type.

I wrote a program for this thread which showed the use of a true array
pointer, but it was rather contrived. As you say, C facilitates the use of
indices rather nicely for array accesses, multi-dimensional or otherwise,
so I didn't post it.
 
K

Keith Thompson

Martin said:
Keith,

I understand what you mean, but K&R2 describes argv as "a pointer to
an array of character strings" (K&R2, p114). I don't think they meant
"array pointer" though - and neither did I!

In my opinion, that's a mistake on their part.

[...]
int arr[10][20];
...
arr[x][y];

arr is of type array 10 of array 20 of int, which decays to pointer to
array 10 of int (there's your pointer to an array).

Whoops, I think you'll find 'arr' decays to "pointer to array of 20 ints."

You're right, of course.
arr[x] is of type array 10 of int, which decays to pointer to int.

arr[x] is of type array 20 of int, which decays to pointer to int,

And again, thanks for the correction.
arr[x][y] is of type int.

Indeed.

[snip]
 
J

jxh

I got some answers here:
http://c-faq.com/aryptr/ptrtoarray.html

but it doe snot tell why should I use "pointer to arrays" ?

If you have ever passed a multi-dimensional array to a
function as a parameter, you used a pointer to an array.

If you wish to dynamically allocate a multi-dimensional
array as a single block of contiguous memory (that is, a
single call to malloc), then it is natural to use a
pointer to an array.

Given merely:

int arr[10];

It would be a rather contrived need to use &arr in code,
but one example would be an API that expected a multi-
dimensional array, and you passed it in letting the API
know there was only one row.

-- James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top