Help with double pointers and const
-
Hi, i cannot figure this out:
typedef struct
{
int a;
int b;
} CARPIC;const CARPIC const* CarPics[] =
{
&Audi,
&BMW,
&Bugatti,
}// Get function
void GetCarPicture(CARPIC** carPicture)
{
int carId= GetCarId(); // Lets say, returns 2
*carPicture = CarPics[carId];
}// Using the function
CARPIC* car;
GetCarPicture(&car);I get the warning:
warning: assignment discards qualifiers from pointer target type
I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.
-
Hi, i cannot figure this out:
typedef struct
{
int a;
int b;
} CARPIC;const CARPIC const* CarPics[] =
{
&Audi,
&BMW,
&Bugatti,
}// Get function
void GetCarPicture(CARPIC** carPicture)
{
int carId= GetCarId(); // Lets say, returns 2
*carPicture = CarPics[carId];
}// Using the function
CARPIC* car;
GetCarPicture(&car);I get the warning:
warning: assignment discards qualifiers from pointer target type
I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.
This would make more sense: ```c++ typedef struct { int a; int b; } CARPIC; CARPIC Audi = { 1, 2 }; CARPIC BMW = { 1, 2 }; CARPIC Bugatti = { 1, 2 }; CARPIC const* CarPics[] = { &Audi, &BMW, &Bugatti, }; // Get function const CARPIC* GetCarPicture(int carId) { return CarPics[carId]; } // Using the function CARPIC const* car; car = GetCarPicture(2);/// ```
-
Hi, i cannot figure this out:
typedef struct
{
int a;
int b;
} CARPIC;const CARPIC const* CarPics[] =
{
&Audi,
&BMW,
&Bugatti,
}// Get function
void GetCarPicture(CARPIC** carPicture)
{
int carId= GetCarId(); // Lets say, returns 2
*carPicture = CarPics[carId];
}// Using the function
CARPIC* car;
GetCarPicture(&car);I get the warning:
warning: assignment discards qualifiers from pointer target type
I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.
-
It should be
void GetCarPicture(const CARPIC const * * carPicture)
{
//...
}
//...
// Using the function
const CARPIC const * car;
GetCarPicture(&car); -
It should be
void GetCarPicture(const CARPIC const * * carPicture)
{
//...
}
//...
// Using the function
const CARPIC const * car;
GetCarPicture(&car);These second
const
s don't appear to make sense: both refer to theCARPIC
value, neither refers to the pointer holding the address. That's also what my compiler states (in a warning). To give some examples:const int *a; // pointer to const int
int const *a; // pointer to const int (same as above!)
const int const *a; // warning C4114: same type qualifier used more than once
int *const a; // const pointer to int
const int * const a; // const pointer to const intSee also http://cdecl.ridiculousfish.com/?q=const+int+*+const+*+a%3B[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
The two
const
s are duplicate. Both refer to the CARPIC value. Neither refers to the pointer. You might also want to check out my comment to CPallinis response in this thread.GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
These second
const
s don't appear to make sense: both refer to theCARPIC
value, neither refers to the pointer holding the address. That's also what my compiler states (in a warning). To give some examples:const int *a; // pointer to const int
int const *a; // pointer to const int (same as above!)
const int const *a; // warning C4114: same type qualifier used more than once
int *const a; // const pointer to int
const int * const a; // const pointer to const intSee also http://cdecl.ridiculousfish.com/?q=const+int+*+const+*+a%3B[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)