0

I have this struct:

struct ChangeIntItem
{
    char *unit;
    const char **parser;
    int *changevalue;
    uint16_t *change_eeprom_value;
    int maximum;
    int minimum;
};

I want to initialize other variables with this struct-Variable:

struct ChangeIntItem ChangeIntItemTypeBoolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = {"off", "on"}};

It is working fine but I get some warnings:

Severity    Code    Description Project File    Line
Warning     braces around scalar initializer    Handsteuerung   C:\Users\...    11

Severity    Code    Description Project File    Line
Warning     (near initialization for 'ChangeIntItemTypeBoolean.parser') Handsteuerung   C:\Users\...    11

Severity    Code    Description Project File    Line
Warning     initialization from incompatible pointer type   Handsteuerung   C:\Users\...    11

Severity    Code    Description Project File    Line
Warning     (near initialization for 'ChangeIntItemTypeBoolean.parser') Handsteuerung   C:\Users\...    11

Severity    Code    Description Project File    Line
Warning     excess elements in scalar initializer   Handsteuerung   C:\Users\...    11

Severity    Code    Description Project File    Line
Warning     (near initialization for 'ChangeIntItemTypeBoolean.parser') Handsteuerung   C:\Users\...    11

In another case I wrote a function which sets the variables of the struct to default-values but I prefer this method because its much shorter.

All mistakes where caused by '.parser = {"off", "on"}' but I don't get my mistake...

0

2 Answers 2

5

You can use compound literals, so change init to

struct ChangeIntItem ChangeIntItemTypeBoolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = (const char *[]){"off", "on"}};

Test

#include <stdio.h>
#include <stdint.h>

struct ChangeIntItem
{
    char *unit;
    const char **parser;
    int *changevalue;
    uint16_t *change_eeprom_value;
    int maximum;
    int minimum;
};

int main()
{
    struct ChangeIntItem ChangeIntItemTypeBoolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = (const char *[]){"off", "on"}};

    printf ("%s - %s\n", ChangeIntItemTypeBoolean.parser[0], ChangeIntItemTypeBoolean.parser[1]);
}

Output

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

3 Comments

Alternatively, parser could be a flexible array member of type const char* parser[]. There are very few cases where pointer-to-pointer variable declarations make sense.
@Lundin Yes, with VLA parser should be last member of struct, shouldn't it?
Thank you! Sometimes its easier than you think ;)
4

Member parser is a pointer.

Change it to an array of pointers, so the initialization can be kept the same:

 const char *parser[2];

Or use a compound literal or another variable:

.parser = ( const char*[]){"off", "on"}

const char* array[2] = {"off", "on"};
.parser = array

1 Comment

@LPs No, it is a pointer. Phrase pointer of pointers doesn't have any meaning.

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.