0

Can I initialize a stuct in C in the following way:

struct database {
  char* Name;
  char* Title;
  char* DOB;
  int EmployeeNo;
} people[100];

people[0] = { "Jon", "Manager", "1-1-1990", 12345 };
people[1] = { "Bob", "Accountant", "1-1-1990", 54321 };

I am using gcc version 9.2.1 20191130 (Debian 9.2.1-21)

2
  • Did you try it? What happened when you did? Commented Mar 3, 2020 at 6:15
  • 2
    Technically, that is not 'initialization' — it is an attempt at assignment. You'd have to use compound literals to be able to assign roughly like that, though: people[0] = (struct database){ "Jon", "Manager", "1-1-1990", 12345 };. Like designated initializers, compound literals are a feature of C99 and later versions of C. Commented Mar 3, 2020 at 6:30

3 Answers 3

4

No, the correct way is:

struct database {
  char* Name;
  char* Title;
  char* DOB;
  int EmployeeNo;
} people[100]={ { "Jon", "Manager", "1-1-1990", 12345 },
                { "Bob", "Accountant", "1-1-1990", 54321 }
              };

This is usable in C89, C90, C94, C99, C11, C17 — and pre-standard C if the initialization is done at file scope.

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

3 Comments

@Yunnosch — The notation here (no designated initializers) is usable in C89, C90, C94, C99, C11, C17 — and pre-standard C if the initialization is done at file scope. C89/C90 (and pre-standard C) may have had limits on how much you could initialize arrays of structures with automatic duration.
@Yunnosch I edited my post and added more information that provided by JonathanLeffler. Thanks.
@behnamarazkhani Thank you! This worked flawlessly for my use case.
2

No, do it like this:

struct database {
    char* Name;
    char* Title;
    char* DOB;
    int EmployeeNo;
};

struct database ppl[5] = {
    { .Name = "Jon",  .Title = "Manager", .DOB = "1-1-1990", .EmployeeNo = 12345 },
    { .Name = "Ravi", .Title = "Manager", .DOB = "1-1-1990", .EmployeeNo = 12345 },
    {.....},
    {.....},
    {.....},
};

Initialization and definition must be done at the same time — otherwise, you're assigning, not initializing.

6 Comments

I think mentioning the (earliest) C standard versin for which this is applicable is necessary. OP did not define one and I am pretty sure that this is not possible for e.g. C89.
@Yunnosch -- note that OP specified gcc 9.2, which defaults to C17, so assuming support for designated initializers seems reasonable.
@exnihilo It is quite a plausible assumption, but it IS an assumption. You know what happens if people "assume", don't you?
@Yunnosch -- It would be more remarkable to find out that OP was compiling in C89 mode with a compiler that defaults to C17; that is more of an assumption than that they are using the default mode. Still, it would be good for the answerer to point out that designated initializers were added to Standard C with C99, even though that was 20 years ago....
@Yunnosch: It's possible to argue that the c tag states "This tag should be used with general questions concerning the C language, as defined in the ISO 9899 standard (the latest version, 9899:2018, unless otherwise specified — also tag version-specific requests with c89, c99, c11, etc)." so there is no need to put qualifying information in place — if someone wants C99 or C90, they should tag accordingly. For many purposes, C11 and C17 are essentially the same.
|
2

Can I initialize a stuct in C in the following way:

Not quite.
There are other ways to initialize as provided by other answers.
You can assign within a function with a compound literal.

people[0] = 
    (struct database){ "Jon", "Manager", "1-1-1990", 12345 };
people[1] = 
    (struct database){ "Bob", "Accountant", "1-1-1990", 54321 };

2 Comments

The assignments must be inside a function, of course; presumably the array definition would be too. In which case, there's a big difference between the assignments and the initialization — with the assignments, people[2]..people[99] have indeterminate values, whereas with initialization, they're zero-initialized.
@JonathanLeffler Agree about differences. A key attribute is that if only 2 of the 100 need assignment, then assignment of 2 is certainly faster than initialization of 100. initialization affords no partial. Either all 100 are initialized of none.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.