2

I'm trying to create an array of structs but keep getting this error. can you please advise?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MODEL_LENGTH 4
#define NUM_OF_DESTINATIONS 8
#define MAX_DESTINATIONS_WORD_LENGTH 12
#define NUM_OF_PLANES 3
typedef struct
{
    char p_model[MODEL_LENGTH];
    char p_destinations[NUM_OF_DESTINATIONS][MAX_DESTINATIONS_WORD_LENGTH];
} airplane;

 airplane  a_737 = { "737", {"Larnaca", "Athens", "Budapest", "Zurich", 
"London", "Paris", "Rome"} };
 airplane  b_747 = { "747", {"London", "New York", "Bangkok"} };
 airplane  c_787 = { "787", {"London", "New York", "Los Angeles", "Hong 
Kong", "Miami"} };

airplane planes_arr[NUM_OF_PLANES] = { a_737, b_747, c_787 };

I used to have numbers instead of #define, after reading posts I changed it but I still don't understand what's wrong here.

update: I tried changing the array to an "airplane *planes_arr[]". I tried typing: airplane *a_ptr = &a_737; and tried to put "a_ptr" in the array and I keep getting the same error.

now, i changed to this: airplane *planes_arr[NUM_OF_PLANES] = { &a_737, &b_747, &c_787}; and no error showed.

can I please get an explanation on the topic?

a_beilis
  • 29
  • 7

2 Answers2

2

Try initializing the whole array with proper constants:

airplane planes_arr[NUM_OF_PLANES] = {
    { "737", {"Larnaca", "Athens", "Budapest", "Zurich", "London", "Paris", "Rome"}},
    { "747", {"London", "New York", "Bangkok"}},
    { "787", {"London", "New York", "Los Angeles", "Hong Kong", "Miami"}}
};

And define the single variables (why not use array elements??) inside a function

int main(void) {
    airplane a_737 = planes_arr[0]; // copy
    airplane b_747 = planes_arr[1];
    airplane c_787 = planes_arr[2];
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • ok, ill try. can you please explain what's the difference? I also tried with pointers (I edited the original post) – a_beilis Oct 30 '18 at 13:53
1

can I please get an explanation on the topic?

This has a wee bit on arrays (cpp though) but is relevant I believe:

https://www.learncpp.com/cpp-tutorial/61-arrays-part-i/

The problem is that "When declaring a fixed array, the length of the array (between the square brackets) must be a compile-time constant" (from learncpp.com)

There is a difference between compiling and running. To get the values of a_737 etc, the program doesn't just need to be compiled, it needs to be run. So the values of a_737... etc are not determined at compile time.

The memory addresses though (i.e. &a_787 etc) are determined by the compiler at compile time.

if you try

airplane *a_ptr = &a_737;
airplane *planes_arr[NUM_OF_PLANES] = { a_ptr, &b_747, &c_787 };

then the assignment to a_ptr must be done at run time. and is not a compile time constant so will run into the same error. C compilers aren't very "smart" as they don't run assignments before compiling.

Even though you may write the assignment before hand, there is no pre-determined order in the global scope for the compiler to understand this. This code however will work in non global scope, such as a function, because they will now be compiled and executed in order.

similar Q/A here?

'Initializer not constant' on global variable?

pm101
  • 1,309
  • 10
  • 30
  • ok i think i understand. what about creating a pointer variable like a_ptr= &a_737 and then assining it to array? its not working for the same reason? – a_beilis Oct 30 '18 at 14:09
  • if I understand correctly then yes, I've updated the answer to reflect that – pm101 Oct 30 '18 at 14:19