-1

I wrote a code which produces the following error:

Thread 1: EXC_BAD_ACCESS (code=1, adress=0x7.....)

I want the program to link the countries, states, cities and shops within an structure. But when I try to run my program it gives me the error you see above.

I already tried deleting the strcpy and the for but the error still occurs. So the error must be within the structures. What is it I'm doing wrong?

#include <stdio.h>
#include <string.h>

#define SMAX 16
#define CMAX 256
#define SHMAX 300

int main() {
    struct country {
        char cname[50];
        struct state {
            char sname[50];
            struct city {
                char cityname[50];
                struct shop {
                    char shopname[50];
                    int countshop;
                } shop[SHMAX];
                int countcity;
            } city[CMAX];
            int countstate;
        } state[SMAX];
    } country;

    // country = Germany;

    strcpy(country.state[0].sname, "bayern");
    strcpy(country.state[1].sname, "berlin");
    strcpy(country.state[0].city[0].cityname, "ingolstadt");
    strcpy(country.state[0].city[0].shop[0].shopname, "westpark");
    strcpy(country.state[0].city[0].shop[1].shopname, "edeka");

    for (int i = 0; i < SHMAX; i++) {
        printf("%s\n", country.state[0].city[0].shop[i].shopname);
    }



    return 0;
}
PeterPan
  • 684
  • 1
  • 10
  • 27
  • Note: there is no need to use different names for members in nested structs. `country.state.city.name` is better to read than `country.state.city.cityname` – too honest for this site Sep 19 '15 at 13:53
  • @Olaf Thanks for that. – PeterPan Sep 19 '15 at 13:55
  • Sorry for my stupid comment and answer earlier. Your code looks fine. Do you get the error when you remove the `strcpy`s and the loop and the `printf`? – Spikatrix Sep 19 '15 at 14:04
  • So my line was right the the & before country and the [0] after shopname? What is the difference between the code you showed me and the code I wrote? And as I said in the question, I tried that. – PeterPan Sep 19 '15 at 14:17

1 Answers1

2

The size of the struct is 69043124 bytes which is too much to fit on the stack.

As thread safety is no concern, the struct could be made static:

int main(void) {
    static struct country {
4566976
  • 2,419
  • 1
  • 10
  • 14