Thread: Initializing array members in a struct

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    Initializing array members in a struct

    Hey guys, stupid syntax question here.
    I'm defining a structure with a few array members which I'm trying to initialize by using a declaration list. It is not working and according to google it seems not to be legal syntax in c.

    The alternative is to declare the members inside a different structure as non-array types, and then declare an array of members inside another the struct, but that complicates my interfacing code a bit.

    Here's my original illegal code to give some semblance of meaning to my ramble
    Code:
    typedef struct    s_format_groups
    {
        const char specifier[][16] =
        {
            {'d', 'D' 'i', 'O', 'u', 'U', 'x', 'X'},
            {'s', 'S', 'c', 'C'},
            {'p'}
        };
        const char length[] = {'h', 'l', 'j', 'z'}; // remember to account for "hh" && "ll" maybe define MAX_COUNT 2 as the max number of flag repeats
        const char flags[] = {'-', '+', ' ', '#', '0'};
        const char width[] = {'*'};
        const char precision[] = {'.'};
    
    }                t_format_groups;
    What would be a good way to achieve what I'm trying to do?
    Last edited by Dren; 08-03-2018 at 10:27 PM. Reason: grammar

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Code:
    #include <stdio.h>
    
    typedef struct s_format_groups {
        const char specifier[3][9];
        const char length[5];
        const char flags[6];
        const char width[2];
        const char precision[2];
    } t_format_groups;
    
    int main() {
        t_format_groups fg = {
            .specifier = {
                "dDiOuUxX",
                "sScC"
                "p"
            },
            .length = "hljz",
            .flags = "-+ #0",
            .width = "*",
            .precision = "."
        };
     
        printf("%s\n", fg.specifier[0]);
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Alternatively:
    Code:
    #include <stdio.h>
     
    struct {
        const char specifier[3][9];
        const char length[5];
        const char flags[6];
        const char width[2];
        const char precision[2];
    } format_groups = {
        {
            "dDiOuUxX",
            "sScC"
            "p"
        },
        "hljz",
        "-+ #0",
        "*",
        "."
    };
     
    int main() {
        printf("%s\n", format_groups.specifier[0]);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    Thank you John!
    I particularly like your second solution, as it much better fits my intended use for the code. I hope this is allowed in pre-c99 mode as I am limited to that for now.

    Big kudos! :thumbsup:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-28-2017, 03:41 PM
  2. Replies: 3
    Last Post: 07-30-2012, 08:12 AM
  3. placing struct members into an array
    By droseman in forum C Programming
    Replies: 5
    Last Post: 01-27-2009, 09:18 AM
  4. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  5. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM

Tags for this Thread