1

So here is code, that i can't understand. The thing i can't understand is how to access to structure "town" from structure counties. Also i don't know how to write in structure town and also how to access them.

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

struct town {
    char name[64];
    unsigned number;
};
struct country {
    char name[64];
    struct town capital;
    struct town *towns;
    unsigned number_of_towns;
};

int main() {
    struct country countries[5];
    for (int i = 0; i < 5; i++) {
        printf("Name of country: ");
        scanf("%s", countries[i].name);
        scanf("%s", countries->towns->name); //this is what i try.
    }

    getchar();
    getchar();


    return 0;
}

2 Answers2

2

How to get a variable of type struct town ?

struct town v ;

How to access it?

v.name[i] --> ith charcter of `name`
v.number

How to read them?

scanf("%s",v.name);
scanf("%u",&v.number);

Now how to access them from other struct?

In case you use pointer struct town * you need something so that you can point to it. You need to gather some more variable which you can use to store your information.

struct town capital;// it doesn't need to point to anyone.

struct town* towns; // I need variable(s) of type struct town so that I can point to it.

towns= malloc(sizeof (struct town)* AS_MANY_AS_YOU_NEED);//suppose you allocate for 4

you need to access them.

so what you do?

You can access each of these struct town type variable like this

town[i] and then you can access them as I have shown before.

Do I need to allocate always more than 1 element?

Ofcourse not. You can have soething like

towns = malloc(sizeof (struct town));

access it like this towns[0] or simply *(towns+0) or *towns

What is allocating ?

Allocating memory to store something from free store of memory.

Help me more!!

Okay so you allocated some more variables in towns i-th of which can be accessed like towns[i] and

it's

  • name can be accessed like towns[i].name .

  • number can be in towns[i].number

  • character of name for a particular town towns[i].name[i]

How to access them from countries?

  struct country {
        char name[64];        //countries[i].name
        struct town capital;  //countries[i].capital.name or countries[i].capital.number
        struct town *towns;   // (countries[i]->towns[i]).name or ...
        unsigned number_of_towns;
    } countries[5];
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Please try if this code can help you.

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

struct town {
    char name[64];
    unsigned number;
};
struct country {
    char name[64];
    struct town capital;
    struct town *towns;
    unsigned number_of_towns;
};

int main() {
    struct country countries[5];
    struct town *twn;
    for (int i = 0; i < 5; i++) {
        printf("Name of country: ");
        scanf("%s", countries[i].name);
        twn = malloc(sizeof(struct town));
        printf("Name of town 0: ");
        scanf("%s", twn->name);
        countries[i].towns = twn;
    }
    printf("*** Print Countries ***\n");
    for (int i = 0; i < 5; i++) {
        printf("Name of country: %s\n", countries[i].name);
        printf("Name of town 0: %s\n", countries[i].towns->name);
    }
    for (int i = 0; i < 5; i++) {
        free(countries[i].towns);
    }

    getchar();
    getchar();
    return 0;
}

Test

Name of country: one
Name of town 0: onetown
Name of country: two
Name of town 0: twotown
Name of country: three
Name of town 0: threetown
Name of country: four
Name of town 0: fourtown
Name of country: five
Name of town 0: fivetown
*** Print Countries ***
Name of country: one
Name of town 0: onetown
Name of country: two
Name of town 0: twotown
Name of country: three
Name of town 0: threetown
Name of country: four
Name of town 0: fourtown
Name of country: five
Name of town 0: fivetown
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424