2

So I have 3 files: main.c,countries.h and countries.c

I declare pointer of the structure called "Country" in the countries.h

I have included the countries.h in countries.c and in main.c

and declared the structure its self in countries.c

countries.h

typedef struct Country* pCountry;

countries.c

struct Country {
    char *name;
    pCity cities;
    int numCities;
    pTerritory countryTerr;
};

now, I want to create array of pointers of the Country structure, using malloc

so I did that:

pCountry countries_array;
countries_array = (pCountry); 
malloc(num_of_countries*sizeof(countries_array));

and to assign pointers to each pointer,even though the malloc, does seems to work I cant

assign pointers to the elements in the array using []:

countries_array[0]= new_pointer;

I get "invalid use of undefine struct country" and "derefrecing pointer to incomplete",

what is the problem with the code?

thanks

5
  • 2
    malloc() returns a value. Commented Nov 21, 2018 at 22:46
  • 2
    malloc itselfs returns the pointer, NULL if there is no memory available like: countries_array = malloc(num_of_countries*sizeof countries_array); Commented Nov 21, 2018 at 22:48
  • 2
    You will want to review: Is it a good idea to typedef pointers?. Commented Nov 21, 2018 at 23:00
  • What is countries_array = (pCountry); supposed to mean? Was that intended to be a type cast of the result of the next line? Commented Nov 21, 2018 at 23:42
  • which file does the error point to, to which line? how declaration of the country_array looks like? Commented Nov 22, 2018 at 2:33

1 Answer 1

1

Looks good. Just assign it to something of the same type, struct Country. Also, as pointed out in the comments, it should be malloc num_of_countries * sizeof struct Country (not the pointer type), which is now correctly dereferenced below as sizeof (*countries_array) which also works.

pCountry countries_array;
countries_array = malloc(num_of_countries * sizeof (*countries_array));
struct Country Jefferson = {"Jefferson", 1,2,3 };
countries_array[0] = Jefferson;

// don't forget to free the memory when no longer needed.
free (countries_array);

If we must put a pointer into this array of structs, we can either dereference the pointer like countries_array[0] = *pointer, or... we could declare countries_array as an array of pointers, instead of an array of structs. Perhaps this is what you may want. Either way, the actual structures have to occupy memory somewhere...

pCountry *countries_array = malloc(num_of_countries*sizeof countries_array);
pCountry j = &Jefferson; // `&`, "address of" operator
countries_array[0] = j; // put a `pointer` into the array...
Sign up to request clarification or add additional context in comments.

2 Comments

Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
On casting malloc(). Since this is tagged "C" we don't (cast) the result of malloc, because it hides important compiler errors if we do. In C++, it is required, so it crops up quite a bit. c-faq.com/malloc/mallocnocast.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.