Initialize const struct with const pointer


bomb

I want to form a struct from a const parameter passed to a function. Since the parameter is const, I guess the struct must also be const. However, it doesn't work with pointers.

Compile the following code (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 using pointers 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 discard const here? So how to initialize a const structure with a const pointer?

Monkey_God

In the first case you are creating a copy of an int. A copy of const int doesn't have to be const, so it works fine. In the second case, you're creating a copy of the pointer to const int and assigning it to a pointer to int - which is not allowed, that's why it doesn't compile.

Related


Initialize a const array with a pointer

Rajman Why is the first line valid and the rest not. I though the first is a shorthand for the second. const char *c = "abc"; // Why valid? const char *b = { 'a' , 'b', 'c', '\0' }; // invalid const int *a = { 1, 2, 3, 0 }; // invalid Shafik Yaghmour In the

Initialize const array inside struct

username #define LENGTH 6 typedef char data_t[LENGTH]; struct foo { const data_t data; ... } ... void bar(data_t data) { printf("%.6s\n", data); struct foo myfoo = {*data}; printf("%.6s\n", foo.data); } I'm trying to use this structure to

Initialize const array inside struct

username #define LENGTH 6 typedef char data_t[LENGTH]; struct foo { const data_t data; ... } ... void bar(data_t data) { printf("%.6s\n", data); struct foo myfoo = {*data}; printf("%.6s\n", foo.data); } I'm trying to use this structure to

Initialize const array inside struct

username #define LENGTH 6 typedef char data_t[LENGTH]; struct foo { const data_t data; ... } ... void bar(data_t data) { printf("%.6s\n", data); struct foo myfoo = {*data}; printf("%.6s\n", foo.data); } I'm trying to use this structure to

Initialize const array inside struct

username #define LENGTH 6 typedef char data_t[LENGTH]; struct foo { const data_t data; ... } ... void bar(data_t data) { printf("%.6s\n", data); struct foo myfoo = {*data}; printf("%.6s\n", foo.data); } I'm trying to use this structure to

Initialize const array inside struct

username #define LENGTH 6 typedef char data_t[LENGTH]; struct foo { const data_t data; ... } ... void bar(data_t data) { printf("%.6s\n", data); struct foo myfoo = {*data}; printf("%.6s\n", foo.data); } I'm trying to use this structure to

Initialize and print const char pointer

k33n I get the following code: const char *newLine = "\n"; printf('Content: %c\n', *newLine); What is happening now is a memory error. Why is this so? Acorn The code crashes with a memory error (segmentation fault) because printfa null-terminated string is ex

Initialize and print const char pointer

k33n I get the following code: const char *newLine = "\n"; printf('Content: %c\n', *newLine); What is happening now is a memory error. Why is this so? Acorn The code crashes with a memory error (segmentation fault) because printfa null-terminated string is ex

pointer to const (or pointer to const)

Wengers I have the following code: #include <iostream> int main(){ int v1 = 20; int *p1 = &v1; int **p2 = &p1; return 0; } What I'm trying to do here is point one pointer to another and it works fine in this case. I make p1 point to a cons

How to initialize const struct with union in C

floating ice I have been trying to solve this problem for several hours. This didn't work, although I found some similar questions. I have a union in a struct. Now, I want to initialize const variables of this structure. struct length { int minutes; int

Declare and initialize const Struct in class header

Wentz I'm looking for a way to declare and initialize a constant struct in a class header file. As you can see, the class is being used by the MFC application. The layers on my MFC dialogs never change, so I want to constantly modify them. I am looking for som

How to initialize const struct with union in C

floating ice I have been trying to solve this problem for several hours. This didn't work, although I found some similar questions. I have a union in a struct. Now, I want to initialize const variables of this structure. struct length { int minutes; int

Use C++ to initialize Struct's variable const char* const*

Justin Thurman I'm trying to solve an encoding problem that needs to return a result with a given struct. The structure is defined as: struct Answer { const char* const* lastNames; unsigned numberOfPeople; } where lastNames is a pointer to last names

Initialize const pointer - initializer list in C++

Spatny, Cuba I'm currently trying to implement a tree structure in C++. I start with the following code: class Tree { Node * const first; Node * last; public: Tree(Node * const root) { first = root; las

Initialize const pointer - initializer list in C++

Spatny, Cuba I'm currently trying to implement a tree structure in C++. I start with the following code: class Tree { Node * const first; Node * last; public: Tree(Node * const root) { first = root; las

C const pointer to const struct array as function parameter

Za'tar How can I make Alt.1 work as expected by passing an array pointer and getting the requested reference to the array in Alt.1? struct mystruct { int id1; int id2; }; const struct mystruct local_struct[] = { {0, 55}, {1, 66}, }; // Alt.1

Convert const struct reference to non-const pointer

Becky I'm trying to figure out how to convert a const struct reference to a non-const struct pointer. I have a struct called Foo: struct Foo { Foo& someFunc() { //do something return *this; } }; I have a method: struct Bar { F

Convert const struct reference to non-const pointer

Becky I'm trying to figure out how to convert a const struct reference to a non-const struct pointer. I have a struct called Foo: struct Foo { Foo& someFunc() { //do something return *this; } }; I have a method: struct Bar { F

What is the cgo type equivalent of a const pointer to a struct?

Tara Sala: I have an external function decal in C: //extern void goCallback(const struct libvlc_event_t*, void*); Defined in go: //export goCallback func goCallback(event unsafe.Pointer, userData unsafe.Pointer) { log.Fatal("TODO goCallback") } When comp

What is the cgo type equivalent of a const pointer to a struct?

Tara Sala: I have an external function decal in C: //extern void goCallback(const struct libvlc_event_t*, void*); Defined in go: //export goCallback func goCallback(event unsafe.Pointer, userData unsafe.Pointer) { log.Fatal("TODO goCallback") } When comp

What is the cgo type equivalent of a const pointer to a struct?

Tara Sala: I have an external function decal in C: //extern void goCallback(const struct libvlc_event_t*, void*); Defined in go: //export goCallback func goCallback(event unsafe.Pointer, userData unsafe.Pointer) { log.Fatal("TODO goCallback") } When comp

What is the cgo type equivalent of a const pointer to a struct?

Tara Sala: I have an external function decal in C: //extern void goCallback(const struct libvlc_event_t*, void*); Defined in go: //export goCallback func goCallback(event unsafe.Pointer, userData unsafe.Pointer) { log.Fatal("TODO goCallback") } When comp

What is the cgo type equivalent of a const pointer to a struct?

Tara Sala: I have an external function decal in C: //extern void goCallback(const struct libvlc_event_t*, void*); Defined in go: //export goCallback func goCallback(event unsafe.Pointer, userData unsafe.Pointer) { log.Fatal("TODO goCallback") } When comp