I want to form a struct from const parameters passed to the function. As the parameters are const, i guess the struct has to be const too. However, it does not work with pointers.
The following code compiles (MinGW 4.9.2 32bit)
struct structType_t {
int b;
};
void func1(const int b) {
const structType_t s={b}; // invalid conversion from 'const int*' to 'int*' [-fpermissive]
// do something with s;
}
but with pointers it doesn't:
struct structType_t {
int* b;
};
void func1(const int* b) {
const structType_t s={b}; // invalid conversion from 'const int*' to 'int*' [-fpermissive]
// do something with s;
}
Why does the compiler try to cast away the const here? So how can i use a const pointer to initialise a const structure?
int* bnotconst int* bconst intandint constis the same type,const int*andint* constare different types.