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.
int main()
{
constint 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
}
constint 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.