473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C FAQs 6.12

FAQ 6.12 http://c-faq.com/aryptr/aryvsadr.html :
int a[10];
a reference to "a" has type "pointer to int", and &a is "pointer to
array of 10 ints".
and it does not give any code on how to get "a pointer to the whole array"
and what is the significance of such a pointer ?

--
http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.

Apr 9 '08 #1
13 1312
On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
and it does not give any code on how to get "a pointer to the whole array"
and what is the significance of such a pointer ?
I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html

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

-- http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.

Apr 9 '08 #2
On Wed, 09 Apr 2008 10:34:10 +0100, arnuld <ar*****@ippiVmail.comwrote:
I got some answers here: http://c-faq.com/aryptr/ptrtoarray.html

but it doe snot tell why should I use "pointer to arrays" ?
It may be useful when you want to walk through arrays of arrays.

--
Martin

Apr 9 '08 #3
arnuld wrote:
>On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
>and it does not give any code on how to get "a pointer to the whole array"
and what is the significance of such a pointer ?

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
Apr 9 '08 #4
Philip Potter wrote:
>
arnuld wrote:
On Wed, 09 Apr 2008 14:28:44 +0500, arnuld wrote:
and it does not give any code on how to get "a pointer to the whole array"
and what is the significance of such a pointer ?
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.
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 almost ran out of '*'s. :-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Apr 9 '08 #5
On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mr*****@iedu.comwrote:
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.

--
Martin

Apr 9 '08 #6
Martin wrote:
>
On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mr*****@iedu.comwrote:
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.
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.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Apr 9 '08 #7
Morris Dovey wrote:
Martin wrote:
>On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mr*****@iedu.comwrote:
>>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.

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
Apr 9 '08 #8
Martin <m@b.cwrites:
On Wed, 09 Apr 2008 12:49:12 +0100, Morris Dovey <mr*****@iedu.comwrote:
>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.
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 9 '08 #9
Philip Potter <pg*@doc.ic.ac.ukwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 9 '08 #10
Keith Thompson wrote:
Philip Potter <pg*@doc.ic.ac.ukwrites:
[...]
>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.
He beat you to it: <op***************@ukmp4792.pbi.global.pvt>

Indeed, your message hadn't hit my server when I wrote mine above.
Apr 9 '08 #11
Philip Potter <pg*@doc.ic.ac.ukwrites:
Keith Thompson wrote:
>Philip Potter <pg*@doc.ic.ac.ukwrites:
[...]
>>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.
He beat you to it: <op***************@ukmp4792.pbi.global.pvt>

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

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 9 '08 #12
On Wed, 09 Apr 2008 17:10:03 +0100, Keith Thompson <ks***@mib.orgwrote:
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.

--
Martin

Apr 10 '08 #13
Martin <m@b.cwrites:
On Wed, 09 Apr 2008 17:10:03 +0100, Keith Thompson <ks***@mib.orgwrote:
>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!
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]

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 10 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Kim André Akerĝ | last post by:
First of all, does anyone know what Jerry is up to? As most of us are aware of already, he let the domain expire (again), and it now shows a placeholder page for Register.com. Secondly, I tried...
16
by: mux | last post by:
Hi Is there anywhere (on the net) I can get the entire comp.lang.c FAQs? I could get only a partial list from Steve Summit's Website. Or is buying the book the only way out? Any pointers in...
5
by: Kent Boogaart | last post by:
Hi, I have some hierarchical data (FAQs) that I would like to bind to. The basic structure is: FAQ Category + Categories + FAQs So an FAQ category has any number of sub-categories and any...
3
by: Kuku | last post by:
Hi, Is C++ Faqs good for beginers, or some initial knowledge is required. I'm pretty good in C. If some good books you know of, please do let me know.
2
by: Dave Kuhlman | last post by:
I've recently implemented an output writer for Docutils that produces .odt/ODF files for oowriter from reST (restructured text). I'd like to feed the Python FAQs through my odtwriter, to find out...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Overview NOTE- This complete article on "Windows Autorun FAQs" applies theoretically to all Windows NT-based OSes till Windows Vista (and probably Vista's successors too)....
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Description NOTE- If you are unfamiliar with the concept of autoruns, then read "Windows Autorun FAQs: Overview". Que-1: How can I safely remove or edit the autorun...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Programs dealing with autoruns Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list programs that help me to view/modify the autoruns...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.