array of structure 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
add a comment |
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 '18 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 '18 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 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 '18 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 '18 at 2:33
add a comment |
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 '18 at 0:28
Tico
2,07712332
2,07712332
asked Nov 21 '18 at 22:44
kal polakal pola
111
111
2
malloc() returns a value.
– wildplasser
Nov 21 '18 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 '18 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 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 '18 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 '18 at 2:33
add a comment |
2
malloc() returns a value.
– wildplasser
Nov 21 '18 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 '18 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 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 '18 at 23:42
which file does the error point to, to which line? how declaration of thecountry_array
looks like?
– Serge
Nov 22 '18 at 2:33
2
2
malloc() returns a value.
– wildplasser
Nov 21 '18 at 22:46
malloc() returns a value.
– wildplasser
Nov 21 '18 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 '18 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 '18 at 22:48
2
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 at 23:00
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 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 '18 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 '18 at 23:42
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 '18 at 2:33
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 '18 at 2:33
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 0:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 0:10
add a comment |
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 '18 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 '18 at 0:10
add a comment |
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 '18 at 7:18
answered Nov 21 '18 at 23:39
hellorkhellork
935
935
Checking right after malloc for out of memory condition is advisable. if (!countries_array) { fprintf(stderr, "out of memory"); exit 1; }
– hellork
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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.
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 '18 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 '18 at 22:48
2
You will want to review: Is it a good idea to typedef pointers?.
– David C. Rankin
Nov 21 '18 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 '18 at 23:42
which file does the error point to, to which line? how declaration of the
country_array
looks like?– Serge
Nov 22 '18 at 2:33