1

I'm in a class right now that works with C and one of my assignments requires that I work with a struct that my professor wrote for us. It's actually two structs, with one struct basically containing an array of the first struct.

Here's what they look like:

typedef struct cityStruct
{
    unsigned int zip;
    char *town
} city;
typedef struct zipTownsStruct
{
    int *towns;
    city **zips;
    city *cities
} zipTowns;

And here's my function for allocating memory for the zipTowns structure:

void getArrs(zipTowns *arrs, int size)
{
    arrs->towns = malloc(sizeof(int) * size);
    arrs->zips = malloc(sizeof(city **) * size);
    arrs->cities = malloc(sizeof(city *) * size);
}

From what I understand, what I'm doing here is allocating space in memory for a certain number of ints, city pointers, and city structures, based on the size variable. I understand that this is basically what an array is.

I'm having trouble with understanding how I can access these arrays and manipulate items in it. Writing this gives me an error:

strcpy(arrs.cities[0]->town, "testTown\0");

You can see what I'm trying to do here. I want to access each "City" in the zipTowns struct by index and insert a value.

How can I access the items in these dynamically allocated array of structures?

3
  • 1
    What does the error say? IOW, is cities an array os structs or an array of pointers? Commented Sep 16, 2019 at 22:01
  • Notice your int * towns allocates sizeof(int) * size, not sizeof(int*) * size. Your allocation pointer arithmetic is off by one reference for the other two fields; your city * * zips should allocate sizeof(city * ) * size and your city * cities should allocate sizeof(city) * size. Commented Sep 16, 2019 at 22:03
  • 1
    I guess the purpose of the class exercise is to teach you how to use pointers. Search online for a good tutorial on them if you cannot follow your trainer Commented Sep 16, 2019 at 22:06

1 Answer 1

3

Think of x->y as (*x).y.

arrs is not a structure, it's a pointer to a structure, and cities is not a pointer to a pointer to a structure, it's just a pointer to a structure.

Use arrs->cities[0].town instead of arrs.cities[0]->town.

However, you're still not allocating enough room for these structures. This should make it clearer what you're doing with the allocations, and should also give you enough room for your data:

arrs->towns = malloc(sizeof(*arrs->towns) * size);
arrs->zips = malloc(sizeof(*arrs->zips) * size);
arrs->cities = malloc(sizeof(*arrs->cities) * size);

With the second and third, you were only allocating enough room for a pointer to be stored instead of the actual data type.

With this approach, you will be able to access from arrs->cities[0] to arrs->cities[9] and you also will be able to access the members of each city by doing arrs->cities[<number>].<member>.

You also do not need to intentionally null-terminate your strings. This is already done for you. Therefore, you can replace "testTown\0" with "testTown".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.