2

I'm trying to get a structure, which contains an integer and a pointer to another structure. This second structure is just an array of 2 strings and an array of 2 numbers.

What am I doing wrong here?

struct folder {
    char **filenames_array;
    int *images_array;
};

struct display {
    int pos_y;
    struct folder current_folder;
};

struct display g_display = {
    .pos_y = 0,
    .current_folder = {
        .filenames_array = {"002.jpg", "003.jpg"},
        .images_array = {0, 0},
    }
};

I'm getting those errors:

error C2143: syntax error : missing '}' before '.'
error C2143: syntax error : missing ';' before '.'
error C2059: syntax error : '.'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
error C2059: syntax error : '}'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '}'
5
  • 1
    That last structure makes absolutely no sense. What are you trying to do there, initialize it? If so, that's completely wrong. You can't just have at it with C, you'll need a good language reference to do it properly, and what you have here suggests you're urgently in need of a better one. Commented Feb 5, 2018 at 17:38
  • Pointers are not arrays in the first place. Commented Feb 5, 2018 at 17:39
  • Second struct should be struct folder *current_folder;? Commented Feb 5, 2018 at 17:47
  • Good reference please? I want to get to your highness so much. Commented Feb 5, 2018 at 18:02
  • I don't understand what you are trying to do, you need to learn C more. Commented Feb 6, 2018 at 6:29

1 Answer 1

1

You can do this with compound literal syntax:

struct display g_display = {
    .pos_y = 0,
    .current_folder = {
        .filenames_array = (char *[]){"002.jpg", "003.jpg"},
        .images_array = (int []){0, 0},
    }
};

Alternately, if you know your arrays will always have two elements, you can keep the current initializer syntax and declare the arrays as actual arrays instead of pointers:

struct folder {
    char *filenames_array[2];
    int images_array[2];
};
Sign up to request clarification or add additional context in comments.

2 Comments

And then one has to really understand what they are doing. This code will work but will make some not-that-trivial allocations.
Then your compiler is probably not supporting c99

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.