a pointer to an array?

B

blue

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
 
K

kleuske

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
 
M

Michael Mair

blue said:
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;

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
 
M

Michael Mair

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
 
B

blue

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
 
M

Martin Ambuhl

blue said:
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));
return 0;
}

1
2
3
4
5
 
B

blue

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
 
M

manoj1978

blue said:
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.
 
F

Flash Gordon

blue said:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):

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 said:
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] );
 
N

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
 
F

Flash Gordon

Flash said:
blue said:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):


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 said:
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] );
 
K

Keith Thompson

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.

[...]
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.
 
K

Keith Thompson

Michael Mair said:
blue said:
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;

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

Kelsey Bjarnason

[snips]

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.
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top