Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:
int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}
Why could it not work? Besides, does anyone know the real usage of
array pointers?
Best regards,
blue 14 2051
blue schreef: Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array.
In C++ i would recommend it. Unfortunately, it's not valid C.
(See also question 1.21.) If the size of the array is unknown, N can in principle be omitted, but the resulting type, "pointer to array of unknown size," is useless.
However, when I try to write a sample program like:
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr; printf( "%d", arrptr[0] ); }
Why could it not work?
Coz' it's not C.
int main() { int *arrptr; int arr[5] = {1,2,3,4,5}; arrptr = arr; printf( "%d", arrptr[0] ); }
Besides, does anyone know the real usage of array pointers?
There are many uses for them. printf, for instance, usually takes an
array pointer as it's first parameter, "%d" in your case. You can
calculate a pointer to any element in the array by arrptr=arr + i.
Except for the fact that they always point to some valid block of
memory (as long as they're in scope), array-pointers do not differ from
any other pointer.
regards,
Kleuske
blue wrote: Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array. (See also question 1.21.) If the size of the array is unknown, N can in principle be omitted, but the resulting type, "pointer to array of unknown size," is useless.
However, when I try to write a sample program like:
#include <stdio.h>
This is _not_ gratuitous.
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr;
What is that? If you have
int *p, i;
then you use
p = &i;
So, your compiler should tell you that
arrptr = arr;
is wrong. Using the above,
arrptr = &arr;
is right.
printf( "%d", arrptr[0] );
Instead of p[0], you can also write *p. So, essentially, you
are trying to print out an array using %d. I hope you are
aware that this is a stupid idea.
What you probably want is
arrptr[0][0]
or, in this case,
(*arrptr)[0]
Notes: You could also use arrptr to access an array N of an
array 5 of int -- maybe this helps you better to understand
the "[0][0]"...
Another thing: To make this program portable, append '\n'
to your last output, i.e. "%d\n" or a separate putchar('\n');
You forgot to
return 0; }
Why could it not work? Besides, does anyone know the real usage of array pointers?
See above. Yes.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. kl*****@xs4all.nl wrote: blue schreef:
Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array.
In C++ i would recommend it. Unfortunately, it's not valid C.
This is wrong.
In comp.lang.c, I do not officially know about C++ but, used
correctly, "this" is definitely valid C -- the reason why
the whole thing went into the _comp.lang.c_ FAQ.
See also my other reply to this thread.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Thanks for your reply, but I have some questions. Do you mean that if I
declared an array pointer, I will never have "array index out-of-bound"
problem? Can C (C++ you meantioned) do such examinations? I think only
object-oriented language, such as JAVA, will do.
Best regards,
blue
blue wrote: Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array. (See also question 1.21.) If the size of the array is unknown, N can in principle be omitted, but the resulting type, "pointer to array of unknown size," is useless.
However, when I try to write a sample program like:
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr; printf( "%d", arrptr[0] ); }
Why could it not work? Besides, does anyone know the real usage of array pointers?
#include <stdio.h>
int main(void)
{
int (*arrptr)[5];
int arr[5] = { 1, 2, 3, 4, 5 };
unsigned i;
arrptr = (int (*)[5]) arr;
for (i = 0; i < sizeof arr / sizeof *arr; i++)
printf("%d\n", (*arrptr)[i]);
return 0;
}
1
2
3
4
5
Hi, Michael:
I have tried what you suggested, it did work! However, It seems that
I have some misunderstanding of array and pointers:
int arr[5] = {1,2,3,4,5};
int (*arrptr)[5];
Does it not mean that the variable "arr" itself is an "implicit"
pointer? So we could use it like *arr, *(arr+1), ... Besides, for an
array, does "&arr" really exist?
Then why I assign arr to arrptr is an illegal operation? Only "arrptr =
&arr" is legal.
Best regards,
blue
blue wrote: Thanks for your reply, but I have some questions. Do you mean that if I declared an array pointer, I will never have "array index out-of-bound" problem? Can C (C++ you meantioned) do such examinations? I think only object-oriented language, such as JAVA, will do.
Best regards,
blue
C will not detect array index out-of-bound.
But compiler will throw a warning if you try to assign address of a
different sized array to the pointer.
On Wed, 12 Oct 2005 07:22:28 +0000, Martin Ambuhl wrote:
[...] int (*arrptr)[5]; int arr[5] = { 1, 2, 3, 4, 5 };
[...] arrptr = (int (*)[5]) arr;
Or just:
arrptr = &arr;
-- http://members.dodo.com.au/~netocrat
On Wed, 12 Oct 2005 00:27:57 -0700, blue wrote:
[...] int arr[5] = {1,2,3,4,5}; int (*arrptr)[5];
Does it not mean that the variable "arr" itself is an "implicit" pointer? So we could use it like *arr, *(arr+1),
In most contexts, the array "arr" decays into a pointer, so generally yes
it can be used as you say. Read the FAQ for more details. This is often
referred to as "the rule", at least in this group.
... Besides, for an array, does "&arr" really exist?
It does, yes. This has been discussed on the list in the past, and
although they are different types, for meaningful conversions[*] we can
say that the value of arr and &arr is always the same.
Then why I assign arr to arrptr is an illegal operation? Only "arrptr = &arr" is legal.
Because they are different and incompatible types. arr is "array 5 of
int" and &arr is "pointer to array 5 of int".
[*] those conversions being at least to void * and probably char *.
i.e. (void *)arr == (void *)&arr
(char *)arr == (char *)&arr
-- http://members.dodo.com.au/~netocrat
blue wrote: Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
<snip>
However, when I try to write a sample program like:
printf requires a prototype otherwise your program invokes undefined
behaviour and anything can happen.
#include <stdio.h>
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr; printf( "%d", arrptr[0] ); }
Why could it not work? Besides, does anyone know the real usage of array pointers?
You should always say what you mean by "does not work". In this case I
assume you mean that the compiler complains at you.
Think about what you would do with a pointer to an int. You would
dereference the pointer, would you not? The same applies to pointers to
arrays, if you want to read what they point to you need to dereference them.
printf( "%d", (*arrptr)[0] );
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it. kl*****@xs4all.nl writes: blue schreef: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array.
In C++ i would recommend it. Unfortunately, it's not valid C.
I don't believe this is one of the areas in which C and C++ differ.
[...] Besides, does anyone know the real usage of array pointers?
There are many uses for them. printf, for instance, usually takes an array pointer as it's first parameter, "%d" in your case.
No, the first argument to printf() is a pointer-to-char, *not* a
pointer-to-array. Specifically, it's a pointer to the first element
of an array of char (a string); the entire array can be accessed via
that pointer.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Flash Gordon wrote: blue wrote:
Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
<snip>
However, when I try to write a sample program like:
printf requires a prototype otherwise your program invokes undefined behaviour and anything can happen.
#include <stdio.h>
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr;
I was still asleep or I would have corrected this to
arrptr = &arr;
printf( "%d", arrptr[0] ); }
Why could it not work? Besides, does anyone know the real usage of array pointers?
You should always say what you mean by "does not work". In this case I assume you mean that the compiler complains at you.
Think about what you would do with a pointer to an int. You would dereference the pointer, would you not? The same applies to pointers to arrays, if you want to read what they point to you need to dereference them. printf( "%d", (*arrptr)[0] );
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Michael Mair <Mi**********@invalid.invalid> writes: blue wrote: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap)[N];" where N is the size of the array. (See also question 1.21.) If the size of the array is unknown, N can in principle be omitted, but the resulting type, "pointer to array of unknown size," is useless. However, when I try to write a sample program like: #include <stdio.h> This is _not_ gratuitous.
int main() { int (*arrptr)[5]; int arr[5] = {1,2,3,4,5}; arrptr = arr;
What is that? If you have int *p, i; then you use p = &i; So, your compiler should tell you that arrptr = arr; is wrong. Using the above, arrptr = &arr; is right.
Yes, that's correct, but it doesn't obviously follow from the example
with a pointer-to-int until you take into account the special rules
for arrays and pointers.
Given the above declarations, the assignment
p = i;
is incorrect because p is a pointer and i is an int.
The assignment
arrptr = arr;
is also incorrect, but both sides of the assignment are pointer types,
just not compatible pointer types. The expression "arr" is an array
name; it's implicitly converted to a pointer to the first element.
But the correct statement is
arrptr = &arr;
because the operand of a unary "&" operator is one of the few contexts
in which an array name is *not* implicitly converted to a pointer.
"&arr" gives you the address of the whole array.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
[snips]
On 12 Oct 2005 00:31:17 -0700, ma*******@gmail.com wrote: C will not detect array index out-of-bound.
There's no guarantee that it won't, AFAIK. It's not _required_ to, but
that's not the same as saying it won't. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Bruno van Dooren |
last post: by
|
204 posts
views
Thread by Alexei A. Frounze |
last post: by
|
28 posts
views
Thread by Wonder |
last post: by
|
1 post
views
Thread by Jeff |
last post: by
|
8 posts
views
Thread by Martin Jørgensen |
last post: by
|
1 post
views
Thread by Tomás |
last post: by
|
17 posts
views
Thread by I.M. !Knuth |
last post: by
|
12 posts
views
Thread by gcary |
last post: by
|
42 posts
views
Thread by xdevel |
last post: by
|
26 posts
views
Thread by aruna.mysore |
last post: by
| | | | | | | | | | |