1

In the following code where I create a struct locally (within main) and then globally (outside main):

Car new = {.name="New Car"};
CarPtr newer = & (Car) {.name = "Newer Car"};

int main(void) {


    printf("New car name: %s\n", new.name);
    printf("Newer car name: %s\n", newer->name);

    // note: all the below will have automatic storage and are initialized on the stack
    // need to allocate memory (or make global if we want longer)
    Car ford = {
        .name = "Ford F-150",
        .price = 25000
    };
}

Is the following understanding correct of where the structs will be stored?

  • The local structs inside a function -- in this case main -- will be stored on the stack -- and so once that function is exited the variables/stack will be unreachable.
  • The global structs are stored in data (where would the pointer be stored?).

Where are the global structs and pointers stored?

David542
  • 104,438
  • 178
  • 489
  • 842

0 Answers0