0

I have the following code so far, I am pretty sure I can make the explicit initialization with multiple statements but I want to learn how to do it with a single one.

#include <stdio.h>
#include <stdlib.h>
#define LUNCHES 5

int main(void)
{
    struct Food
    {
        char *n;  /* “n” attribute of food */
        int w, c; /* “w” and “c” attributes of food */
    }

    lunch[LUNCHES], 
    lunch[0] = {"apple pie", 4, 100}, 
    lunch[1] = {"salsa", 2, 80};
}

I am thinking the following would work but it is another statement.

 int main(void)
 {
     struct Food
     {
         char *n;  /* “n” attribute of food */
         int w, c; /* “w” and “c” attributes of food */
     }

     lunch[LUNCHES];
     lunch[0] = {"apple pie", 4, 100};
     lunch[1] = {"salsa", 2, 80};
2
  • 2
    Side note: You got your answer below, but for your future benefit, to assign a struct to a variable, this is the syntax: lunch[0] = (struct Foot){"apple pie", 4, 100}; which was introduced in C99. Commented Nov 12, 2012 at 19:00
  • Right that is a good point too. Commented Nov 12, 2012 at 19:06

4 Answers 4

3

You are almost there:

 = { [0] = {"apple pie", 4, 100}, [1] = {"salsa", 2, 80} }

would be an initialization for your array.

This is only if your compiler supports "designated" initializers that came with C99.

otherwise

 = { {"apple pie", 4, 100}, {"salsa", 2, 80} }

would also do.

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

6 Comments

Oh. I see, thanks... it makes sense. I was getting the conflicting error because I had multiple lunch[]
+1: I had no idea the former sample was even possible in C. Please tell me it was added long-after I learned the base-language (C89). I can think of about a zillion places I could have used this since then. The latter I was already aware of.
@WhozCraig, it was a GCC extension for a while, and was added to the C99 standard.
@dbaupp Thanks. Good to know I was only sleeping at the wheel and not outright ignorant on how to drive. Now i need to go see what else I missed in that standard update. Thanks again.
@WhozCraig, C99 is even replaced now by c11 :) And they add a lot of little things that make daily life easier, initializers that are arbitrary expressions (not only constants), for-loop variables, compound literals, ...
|
2

Try:

struct { ... } lunch[LUNCHES] = {{"apple pie", 4,100}, {"salsa",2,80}};

Comments

2

you could define in this way

int main(void)
{
    struct Food
    {
        char *n;                                                            /* “n” attribute of food */
        int w, c;                                                  /* “w” and “c” attributes of food */
    }lunch[LUNCHES] = { {"apple pie", 4, 100}, {"salsa", 2, 80}};
}

Comments

1

Something like this perhaps?

#include <stdio.h>
#include <stdlib.h>

#define LUNCHES 5

struct Food {
   char *n;   /* “n” attribute of food */
   int w, c;  /* “w” and “c” attributes of food */
} lunch[LUNCHES] = {
    {"apple pie", 4, 100}, 
    {"salsa", 2, 80}
};

int 
main(void)
{
  printf ("lunches[0].n= %s\n", lunches[0].n);
  return 0;
}

Comments

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.