array of structure pointers
up vote
2
down vote
favorite
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
c pointers
add a comment |
up vote
2
down vote
favorite
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
c pointers
2
malloc() returns a value.
– wildplasser
Nov 21 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);
– Tom Kuschel
Nov 21 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 at 23:00
What iscountries_array = (pCountry);
supposed to mean? Was that intended to be a type cast of the result of the next line?
– Barmar
Nov 21 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 at 2:33
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
c pointers
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
c pointers
c pointers
edited Nov 22 at 0:28
Tico
2,05312332
2,05312332
asked Nov 21 at 22:44
kal pola
111
111
2
malloc() returns a value.
– wildplasser
Nov 21 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);
– Tom Kuschel
Nov 21 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 at 23:00
What iscountries_array = (pCountry);
supposed to mean? Was that intended to be a type cast of the result of the next line?
– Barmar
Nov 21 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 at 2:33
add a comment |
2
malloc() returns a value.
– wildplasser
Nov 21 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);
– Tom Kuschel
Nov 21 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 at 23:00
What iscountries_array = (pCountry);
supposed to mean? Was that intended to be a type cast of the result of the next line?
– Barmar
Nov 21 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 at 2:33
2
2
malloc() returns a value.
– wildplasser
Nov 21 at 22:46
malloc() returns a value.
– wildplasser
Nov 21 at 22:46
2
2
malloc
itselfs returns the pointer, NULL if there is no memory available like: countries_array = malloc(num_of_countries*sizeof countries_array);
– Tom Kuschel
Nov 21 at 22:48
malloc
itselfs returns the pointer, NULL if there is no memory available like: countries_array = malloc(num_of_countries*sizeof countries_array);
– Tom Kuschel
Nov 21 at 22:48
2
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 at 23:00
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 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?– Barmar
Nov 21 at 23:42
What is
countries_array = (pCountry);
supposed to mean? Was that intended to be a type cast of the result of the next line?– Barmar
Nov 21 at 23:42
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 at 2:33
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 at 2:33
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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...
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
On castingmalloc()
. Since this is tagged "C" we don't (cast) the result ofmalloc
, 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
– hellork
Nov 22 at 0:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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...
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
On castingmalloc()
. Since this is tagged "C" we don't (cast) the result ofmalloc
, 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
– hellork
Nov 22 at 0:10
add a comment |
up vote
1
down vote
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...
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
On castingmalloc()
. Since this is tagged "C" we don't (cast) the result ofmalloc
, 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
– hellork
Nov 22 at 0:10
add a comment |
up vote
1
down vote
up vote
1
down vote
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...
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...
edited Nov 23 at 7:18
answered Nov 21 at 23:39
hellork
1035
1035
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
On castingmalloc()
. Since this is tagged "C" we don't (cast) the result ofmalloc
, 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
– hellork
Nov 22 at 0:10
add a comment |
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
On castingmalloc()
. Since this is tagged "C" we don't (cast) the result ofmalloc
, 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
– hellork
Nov 22 at 0:10
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 at 23:46
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– hellork
Nov 22 at 0:10
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– hellork
Nov 22 at 0:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421469%2farray-of-structure-pointers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
malloc() returns a value.
– wildplasser
Nov 21 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);
– Tom Kuschel
Nov 21 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 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?– Barmar
Nov 21 at 23:42
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 at 2:33