4

Im trying to declare and array in a structure called bread, but it keeps giving me the error expected ';' at end of declaration list when i already have one.

typedef struct
{
   char bread[9][35] = {
   "1. 9-Grain Wheat", 
   "2. 9-Grain Honey Oat", 
   "3. Italian", 
   "4. Italian Herbs & Cheese",
   "5. Flatbread (not baked in restaurant)", 
   "6. Cheese Bread", 
   "7. Hearty Italian", 
   "8. Parmesan Oregano", 
   "9. Roasted Garlic" };        
} subway;

this is the contents of the header file the structure is in

Andrew Russo
  • 69
  • 1
  • 1
  • 4

4 Answers4

8

You can't initialize a structure in a typedef. You have to do it when you define a variable of that type:

typedef struct
{
   char bread[9][50];   // the longest string is 38 characters, so the second dimension 
                        // should be at least 39 (38 plus the null terminating byte)
                        // we'll round up to 50 to leave space for expansion
} subway;

subway s = {{
   "1. 9-Grain Wheat",
   "2. 9-Grain Honey Oat",
   "3. Italian",
   "4. Italian Herbs & Cheese",
   "5. Flatbread (not baked in restaurant)",
   "6. Cheese Bread",
   "7. Hearty Italian",
   "8. Parmesan Oregano",
   "9. Roasted Garlic"
}};
dbush
  • 205,898
  • 23
  • 218
  • 273
3

A typedef is a type definition, it is not a variable declaration. It does not make any sense to initialize a type.

You should be doing this:

typedef struct
{
   char bread[9][LARGE_ENOUGH];
} subway_t;

...

subway_t sub = { /* initialization */ };
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Your strings are too long - turn on compiler warnings (highest level)

'bread' : array bounds overflow

Try

char bread[9][40] = {

instead.

Updated question:

No need to put that single array inside a struct. If you really need to use this inside a typedef, only use char bread[9][40];, and initialize the array outside the typedef:

typedef struct
{
   char bread[9][40];
} subway;

subway mySubway = { /* initialize strings here */ };
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
-2

Your initialization is false : you're trying to initialize a char [][] with a (char *) []. you should use strncpy to initilize such an array

gRRosminet
  • 137
  • 1
  • 8
  • 1
    Initialization is not done with strcpy functions... and this is not the problem here... and [`strncpy` is a dangerous function that should not be used](http://stackoverflow.com/questions/2114896/why-are-strlcpy-and-strlcat-considered-insecure).. and there is no `char(*)[]` in the code... – Lundin Jan 13 '16 at 14:19