problem with pointer

Can anyone tell me what the difference between

int* pt[2] and int (*pt1)[2] is?

I've been playing around w/ that and only found out one difference.
That is, suppose I have a two dimensional array ar[2][2].
I'd be able to do something like: pt1 = ar;
But I'd have to put: pt[0] = ar[0];

Last edited on
int* pt[2] Creates 2 pointers. Each pointer points to a type int

int (*pt)[2] Creates 1 pointer. That pointer points to a type int[2] (ie: an array of 2 ints)


Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef int int2[2];  // typedef to clarify it

int2 foo;  // this is the same as int foo[2];
foo[0] = 100;
foo[1] = 200;

int2* ptr;  // this is the same as  int (*ptr)[2];
ptr = &foo;

cout << (*ptr)[1]; // outputs "200"

// with a multidimentional array
int i_hate_md_arrays[3][2];

ptr = i_hate_md_arrays;  // is the same as   ptr = &i_hate_md_arrays[0];

ptr[1][0] = 5;  // ptr[1][0] accesses the same memory as i_hate_md_arrays[1][0] 
Last edited on
Thanks, that makes sense to me! :)

I just have one more question. I thought foo itself was already the address of the array.
Why would I have to put & before it to get the address?
array names are not the same thing as pointers.

Okay...

think of arrays as being their own type. Like int foo[2]; is really of type int[2], which is different from type int.

So:

1
2
3
4
5
6
7
8
9
10
11
12
typedef int int2[2];  // typedef

// pointers to arrays work like pointers to any other type
//  a point to an int looks like this:
int an_int;
int* a_ptr;
a_ptr = &an_int;

// so pointers to arrays would work exactly the same:
int2 an_array;
int2* a_ptr;
a_ptr = &an_array;



Where this gets tricky is that C and C++ let you implicitly cast an array name to a pointer of the element type. So if you have an int2 type, you can cast an int2 to an int*:

1
2
3
4
int2 an_array;
int* a_ptr;
a_ptr = an_array;  // OK
a_ptr = &an_array[0];  // equivilent way to do the same thing 



So while array names are not really the same as pointers, you can cast them to pointers. Note however that they cast to the element type, not the array type.

IE: int2 is an array of ints, so you can get a pointer to an int, but you can't get a pointer to an int2 without the & operator.


EDIT: corrected my code.. forgot a * somewhere
Last edited on
Disch does a great job explaining this. But because I want to drop my two cents in; here I go. When you use pointers they are seen by the compiler as you wanting to work with the memory address of the value you are pointing to.

So Disch's assignment of ptr = &foo; is not valid because it is trying to assign the array of 2 to the one address of ptr, this is what we call a typo. It should say ptr = foo; which would assign the pointer to the beggining of the array with out the need to specify [0].

He does it again at aptr /*(sic)*/ = &an_int; but this is ok because he is not assigning the pointer to an array but rather an individual int value, so he would need to specify that he is assigning the address held by an_int to aptr (sic).

So in this regard pointers can be seen as similar to array's but remember they are not quite the same.

EDIT: Correct me if I'm wrong but in the above example 'foo' is a pointer created for the array by default as opposed to 'foo[0]' which specifies a variable in the array.

RE-EDIT: I just tested this theory and it is wrong to make this assumption.
Last edited on
So Disch's assignment of ptr = &foo; is not valid because it is trying to assign the array of 2 to the one address of ptr, this is what we call a typo.


No. My code is correct.

The only typos I had were these:

1
2
3
4
5
6
7
8
9
10
// pointers to arrays work like pointers to any other type
//  a point to an int looks like this:
int an_int;
int* a_ptr;
a_ptr = &an_int;  // <- I had 'aptr' instead of a_ptr

// so pointers to arrays would work exactly the same:
int2 an_array;
int2* a_ptr;  //<- I forgot the * here
a_ptr = &an_array;


But I corrected those errors. The rest of it is 100% correct.

Correct me if I'm wrong but in the above example 'foo' is a pointer created for the array by default as opposed to 'foo[0]' which specifies a variable in the array.


'foo' is an array name, not a pointer. It can be cast to a pointer, though.

You may want to read my post again.
Ah I gotcha, I see that you declared the pointer to foo as an int2 as well (this makes it an array of pointers?)! I wasn't expecting that so I guess I glanced over it. I caught myself on the EDIT with my RE-EDIT, I just didn't delete it.
(this makes it an array of pointers?)


Not quite.. it makes it a pointer to an array.

An array of ints is a different type than just an int. That's kind of the whole thing with my typedef example. int2 is it's own type (and that type is an array of 2 ints), and you can have a pointer to an int2 just like you have a pointer to anything else.
Topic archived. No new replies allowed.