declaration of pointer pointing to arrays.

X

xian_hong2046

Hello,

Could someone please tell me what's the correct way of representing a
pointer pointing to an array?

If I have an array of int, then I have:

int a[3];

I understand I can create a pointer pointing to "a" by:

int* b = a;

However, according to the book Thinking in C++, one can read pointer
types using a right->left->right approach. An example is:

int (*(*f4())[10]) ()

and "f4" is a function that returns a pointer, pointing to an array of
10 elements, where each element is a function that returns an int.

Following this, a pointer to an array of ints should be:

int (*c)[]

However, this is wrong: the compiler complains about it. The two
methods of declaring pointer types seem inconsistent, or, am I missing
something?

Thanks a lot!
xian
 
N

Noah Roberts

Following this, a pointer to an array of ints should be:

int (*c)[]

However, this is wrong: the compiler complains about it. The two
methods of declaring pointer types seem inconsistent, or, am I missing
something?

You're missing a size for one. You can't have an unsized array
pointer. Where would ++ take it?

I believe the syntax is right but you just need to provide a size. I
hardly ever need to do this though so I could easily be wrong...I look
it up every time I do have to.
 
X

xian_hong2046

Hi Noah,

I tried to have: int (*c)[3] = a;

The compiler then complained with: can't convert int* to int(*)[3].
What could be the problem?

I also tried: int ((*c)[3]) = a;
but the problem remained.

Thanks,
xian
 
J

Jakob Bieling

I tried to have: int (*c)[3] = a;

The compiler then complained with: can't convert int* to int(*)[3].
What could be the problem?

'a' is an array and 'c' is a pointer to an array. Think in terms of
simpler types: let 'a' be an 'int' and 'c' be a pointer to 'int':

int a;
int* c = a; // error

In other words, you need to get the address of the variable and
assign that:

int* c = &a;

and in your particular case:

int a [3];
int (*c) [3] = &a;

hth
 
X

xian_hong2046

Hi Jakob,

Yes, it worked after I changed it to &a, thanks!

However, I'm still confused by array pointers. I've checked that if
"a" is an array, then "a", "&a", "&a[0]" all have the value of the
first "int" element in array "a". In that case, why can I say:

int* c = a;
and
int* b = &a[0];

but not
int* d = &a

As you said "a" is an array, so &a is the address of that array, which
is the same as the address of the first element. Consequently, I'd
expect they can be assigned to the pointer of type int*. This looks
really confusing to me...

Thanks.
xian
 
J

Jakob Bieling

However, I'm still confused by array pointers. I've checked that if
"a" is an array, then "a", "&a", "&a[0]" all have the value of the
first "int" element in array "a". In that case, why can I say:

int* c = a;

int[] implicitly decays to a pointer. So it is ok.
and
int* b = &a[0];

'&a[0]' is the same as '&*(a + 0)', so the type is equal to int* as
well.
but not
int* d = &a

'a' has the type 'int[3]', so '&a' obviously has the type 'int (*)
[3]'. This cannot be converted to 'int*', which is why you get an error.
As you said "a" is an array, so &a is the address of that array, which
is the same as the address of the first element. Consequently, I'd
expect they can be assigned to the pointer of type int*. This looks
really confusing to me...

The fact that the address of the first element in the array is the
same as the address of the whole array does not matter here. It is the
difference in type that matters. C++ is a very type-safe language and
does not allow such conversions by default.

hth
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top