2

I was wondering if it is possible in C to initialise a structure in a following way:

struct Test* test = { .int_value = 10, .char_value = 'c', .pointer_to_float_value = (float*)1.512 };

If I try to do this with a structure defined in a way:

struct Test
{
    int int_value;
    char char_value;
    float* pointer_to_float_value;
};

I get an error for all elements of the structure:

error: field name not in record or union initializer

Is there a way to bypass this issue?

2
  • Doesn't seem to be working for me. The error 'error: cannot convert to a pointer type' appears when I run it with programiz.com/c-programming/online-compiler Commented Jan 17, 2022 at 8:32
  • Overall you seem to be struggling with pointers. You cannot "store data in pointers", which is a recurring problem with this code. Pointers can only be used to point at data allocated elsewhere. Commented Jan 17, 2022 at 8:50

3 Answers 3

3

This syntax

struct Test* test { .int_value = 10, .char_value = 'c', .pointer_to_float_value = (float*)1.512 };

is invalid.

Instead you could use a compound literal as for example

float f = 1.512f;
struct Test* test = &( struct Test ){ .int_value = 10, .char_value = 'c', .pointer_to_float_value = &f };

From the C Standard (6.5.2.5 Compound literals)

5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.

Sign up to request clarification or add additional context in comments.

10 Comments

The problem here is that the structure will be a temporary whose lifetime will be over just after the initialization, so the test pointer will point at unallocated memory.
@nielsen You are mistaken. There is no problem. The lifetime of the compound literal is the scope where it is defined,
This is actually working as intended. Is there a way to avoid having to define a variable 'f' for pointer_to_float? I would also like to avoid that.
@Amaterastis No, you may not take an address of an integer constant. You need to have an lvalue.
@VladfromMoscow You are right (as usually :-) ). Thank you for adding the C Standard reference. It is important to know the lifetime of the entity pointed at.
|
3

It is indeed possible, but you are declaring a pointer to a struct and trying to initialize it as a struct (not pointer). The same issue with the pointer to float. The following should work:

    float a = 1.512;
    struct Test test_struct = { .int_value = 10, .char_value = 'c', .pointer_to_float_value = &a };
    struct Test *test = &test_struct;

3 Comments

I explicitly want struct Test* test definition. This is not the same.
@Amaterastis to have a pointer, you need a structure to point at, so it is a question of subsequently declaring a pointer to point at the struct. I will update the answer with this.
@Amaterastis This is the correct way of initializating a struct. It's entirely unclear why your code is using a pointer and what you hope to achieve with that.
1

There are a number of problems with your code. You don't have a = sign and you are not handling pointers and types correctly.

It can be done like:

struct Test * test = &(struct Test){ .int_value = 10, 
                                     .char_value = 'c',
                                     .pointer_to_float_value = &(float){1.512f} };

Notice how it uses (struct Test) to set the type of the compound literal and & to get a pointer to it. The same applies to the float pointer.

4 Comments

I manually typed it in, so I forgot. Sorry. Can you please explain to me why this syntax &(float){1.512f} is working? I do not really understand it. (meaning I do not understand why you use { } these brackets).
It gives you an unnamed float variable with the value 1.512 and then you take the address using & to get a pointer to the unnamed variable.
Understood. So { } are a part of this syntax? I did not see that before.
@Amaterastis The syntax is for "compound literals" that have been part of the standard since 1999 (C99). They behave like unnamed variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.