Structures in C with the error "no member named ..."

I am trying to create a structure containing the country, state, city and name of the local store. Sorry, I get this error:

No member named bavaria in country structure

So it seems like an error is happening here: strcpy(germany.bavaria.ingolstadt.westpark, "Westpark");

What am I doing wrong?

This is my complete code:

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

int main() {

    struct country {
        char countryname[100];
        struct state {
            char statename[100];
            struct city {
                char cityname[100];
                int postal;
                struct shop {
                    char shopname[100];
                } shop;
            } city;
        } state;
    } country;

    struct country germany;
    struct state bavaria;
    struct city ingolstadt;
    struct shop westpark;

    strcpy(germany.countryname, "Germany");
    strcpy(germany.bavaria.ingolstadt.westpark, "Westpark");

    return 0;
}
+4
source share
5 answers

When you write struct Yin this context

struct X {
    struct Y {
        int z;
    } y;
} x;

you do two things:

  • Define struct Yand
  • Add a ytype field struct Yinside struct X.

struct, , . struct , struct country .

, , :

// This is what the structure dictates, probably not what you want
struct country westpark;
strcpy(westpark.countryname, "Germany");
strcpy(westpark.state.statename, "Bavaria");
strcpy(westpark.state.city.cityname, "Ingolstadt");
strcpy(westpark.state.city.shop.shopname, "Westpark");

, . , - :

struct country {
    char countryname[100];
    struct state {
        char statename[100];
        struct city {
            char cityname[100];
            int postal;
            struct shop {
                char shopname[100];
            } shop[MAX_SHOP]; // maybe 128
            int shopCount;
        } city[MAX_CITY];     // Around 256
        int cityCount;
    } state[MAX_STATE];       // Probably 16
    int stateCount;
} country;

, , , - . , .. stateCount , state[] , cityCount state[] city[], ..

struct 50 , : , -, 50 - . struct:

strcpy(country.countryname, "Germany");
country.stateCount = 1; // For Bavaria
strcpy(country.state[0].statename, "Bavaria");
country.state[0].cityCount = 1; // For Ingolstadt 
strcpy(country.state[0].city[0].cityname, "Ingolstadt");
country.state[0].city[0].shopCount = 1; // for Westpark
strcpy(country.state[0].city[0].shop[0].shopname, "Westpark");

, , . , state[], , city[], , , . , .

+1

, , :

struct shop {
    char shopname[100];
};

struct city {
    char cityname[100];
    int postal;
    struct shop shop;
};

struct state {
    char statename[100];
    struct city city; 
};

struct country {
    char countryname[100];
    struct state state;
};

:

struct country germany;
struct state bavaria;
struct city ingolstadt;
struct shop westpark;
strcpy(germany.bavaria.ingolstadt.westpark, "Westpark");

: struct country , bavaria. state. :

strcpy(germany.state.city.shop.shopname, "Westpark");

, , , :

struct country germany;
strcpy(germany.countryname, "Germany");
strcpy(germany.state.statename, "Bavaria");
strcpy(germany.state.city.cityname, "Ingolstadt");
strcpy(germany.state.city.shop.shopname, "Westpark");
+3

. .

strcpy(germany.state.city.shop, "Westpark");

strcpy(westpark, "Westpark");

, - . . // - . .

, -, 1: n. , , , . .

+1

Variables bavaria, ingolstadtand westparkare separate elements, and not members of the structure country.

strcpy(germany.state.city.shop.shopname, "Westpark");

may work (but maybe not do what you intend to).

+1
source

Depending on how your structure is defined, you will need to do this:

strcpy(germany.countryname, "Germany"); 
strcpy(germany.state.statename, "Bavaria");
strcpy(germany.state.city.cityname, "ingolstadt");
strcpy(germany.state.city.shop.shopname, "Westpark");

The best way to determine the structure would be as follows:

   struct shop {
        char countryname[100];
        char statename[100];
        char cityname[100];
        int postal;
        char shopname[100];
    };

Then you can do this:

struct shop myshop;

strcpy(myshop.countryname, "Germany"); 
strcpy(myshop.statename, "Bavaria");
strcpy(myshop.cityname, "ingolstadt");
strcpy(myshop.shopname, "Westpark");
+1
source

Source: https://habr.com/ru/post/1607820/


All Articles