How to Make a pointer to an array.

Write your question here.
Hey everyone. Basically this. Im trying to First declare a pointer to an array of Dice-object (Dice being my class). Then i have to create an array with 5 dice-objects.

This is what Ive got so far.
1
2
  Dice dices[5];
	Dice *pD1 = dices;


I think this is how you declare a pointer to an array but Im not 100% sure. It says here tip: A pointer to an array is a pointer to the first element in an array.


Any help would be appriciated, thank you!
Looks okay to me mate.
have a look here as well:
http://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm

(although if this is a c++ course you'd use cout for output and not printf).
Last edited on
> A pointer to an array is a pointer to the first element in an array.

No.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
    const int N = 4 ;

    int a[N] = { 10, 11, 12, 13 } ; // array of N int

    ////////////// pointer to first element of array /////////////

    int* pf1 = &( a[0] ) ; // pointer to first element of array
    std::cout << pf1 << '\n' ; // print pointer to int
    std::cout << *pf1 << '\n' ; // print value of first element of array (int)
    *pf1 = 99 ; // assign to first element of array
    std::cout << *pf1 << '\n' ; // print value of first element of array (int)

    // implicit conversion: array to pointer to the first element of the array
    // this is the simplest way to get a pointer to the first element of an array
    int* pf2 = a ; // pointer to first element of array


    ////////////// pointer to  array /////////////
    decltype(a)* pa1 = &a ; // pointer to array of N int

    int (*pa2)[N] = &a ; // pointer to array of N int
    std::cout << pa2 << '\n' ; // print pointer to array
    // *pa1 = 99 ; // **** error: *pa1 is an (a reference to the) array


    ////////////// pointer to first element of array /////////////
    int* pf3 = &( (*pa1)[0] ) ; // pointer to first element of array

   // implicit conversion: array to pointer
    int* pf4 = *pa2 ; // pointer to first element of array

}
Last edited on
1
2
3
4
5
6
7
8
9
10
int main()
{

	const int N = 4;
	int a[N] = { 10, 11, 12, 13 }; // array of N int
	int* p = a;  // pointer to an array

	std::cout << *p << std::endl;

}	


That's the first element in the array is it not? Why are you trying to confuse OP? Or why are you trying to confuse me? :)
Last edited on
> Why are you trying to confuse OP?

There is no confusion.

1
2
3
4
5
6
7
const int N = 4;
int a[N] = { 10, 11, 12, 13 }; // array of N int

int* p = a;  // pointer to an array the first element of the array

auto q = &a ; // pointer to the array
// note: p and q are not the same; their types are different. 

i can see what you are trying to explain now.
The question is, whether the OP can see it too.

Actually, the question is who wrote that "tip":
A pointer to an array ...

And whether they made a semantic error.
Topic archived. No new replies allowed.