Returning incorrect data type?
I've encountered an error when I'm returning a pointer to a substructure in a structure. The error is as follows.
evolve.c:28:21: error: incompatible types when returning type ‘PPM_IMAGE {aka struct <anonymous>}’ but ‘PPM_IMAGE * {aka struct <anonymous> *}’ was expected
return (indPointer->image);
Everything is the same yet the only difference are the extra asterisks (meaning something to do with me returning pointers incorrectly). Here are snippets of the code:
Function:
PPM_IMAGE *evolve_image (const PPM_IMAGE *image, int num_generations, int population_size, double rate);
Return statement:
return (indPointer->image);
Declaration of indPointer:
Individual *indPointer = generate_population(population_size, image->width, image->height, image->max_color);
Structure of Individual:
typedef struct {
PPM_IMAGE image; // image
double fitness; // fitness
} Individual;
As you can see I'm trying to access the first element of the array being pointed at by indPointer. Of this first element I'm trying to return the image substructure within the Individiual.
c pointers return structure genetic-algorithm
add a comment |
I've encountered an error when I'm returning a pointer to a substructure in a structure. The error is as follows.
evolve.c:28:21: error: incompatible types when returning type ‘PPM_IMAGE {aka struct <anonymous>}’ but ‘PPM_IMAGE * {aka struct <anonymous> *}’ was expected
return (indPointer->image);
Everything is the same yet the only difference are the extra asterisks (meaning something to do with me returning pointers incorrectly). Here are snippets of the code:
Function:
PPM_IMAGE *evolve_image (const PPM_IMAGE *image, int num_generations, int population_size, double rate);
Return statement:
return (indPointer->image);
Declaration of indPointer:
Individual *indPointer = generate_population(population_size, image->width, image->height, image->max_color);
Structure of Individual:
typedef struct {
PPM_IMAGE image; // image
double fitness; // fitness
} Individual;
As you can see I'm trying to access the first element of the array being pointed at by indPointer. Of this first element I'm trying to return the image substructure within the Individiual.
c pointers return structure genetic-algorithm
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
1
Individual
is astruct
with a componentPPM_IMAGE image;
,indPointer
a pointer toIndividual
. So, when you doindPointer->image
you get an expression of typePPM_IMAGE
. You need an expression ofPPM_IMAGE*
. To get the proper type, you had to doreturn &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.
– Scheff
Nov 25 '18 at 8:04
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an&
:return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.
– Jonathan Leffler
Nov 25 '18 at 8:14
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12
add a comment |
I've encountered an error when I'm returning a pointer to a substructure in a structure. The error is as follows.
evolve.c:28:21: error: incompatible types when returning type ‘PPM_IMAGE {aka struct <anonymous>}’ but ‘PPM_IMAGE * {aka struct <anonymous> *}’ was expected
return (indPointer->image);
Everything is the same yet the only difference are the extra asterisks (meaning something to do with me returning pointers incorrectly). Here are snippets of the code:
Function:
PPM_IMAGE *evolve_image (const PPM_IMAGE *image, int num_generations, int population_size, double rate);
Return statement:
return (indPointer->image);
Declaration of indPointer:
Individual *indPointer = generate_population(population_size, image->width, image->height, image->max_color);
Structure of Individual:
typedef struct {
PPM_IMAGE image; // image
double fitness; // fitness
} Individual;
As you can see I'm trying to access the first element of the array being pointed at by indPointer. Of this first element I'm trying to return the image substructure within the Individiual.
c pointers return structure genetic-algorithm
I've encountered an error when I'm returning a pointer to a substructure in a structure. The error is as follows.
evolve.c:28:21: error: incompatible types when returning type ‘PPM_IMAGE {aka struct <anonymous>}’ but ‘PPM_IMAGE * {aka struct <anonymous> *}’ was expected
return (indPointer->image);
Everything is the same yet the only difference are the extra asterisks (meaning something to do with me returning pointers incorrectly). Here are snippets of the code:
Function:
PPM_IMAGE *evolve_image (const PPM_IMAGE *image, int num_generations, int population_size, double rate);
Return statement:
return (indPointer->image);
Declaration of indPointer:
Individual *indPointer = generate_population(population_size, image->width, image->height, image->max_color);
Structure of Individual:
typedef struct {
PPM_IMAGE image; // image
double fitness; // fitness
} Individual;
As you can see I'm trying to access the first element of the array being pointed at by indPointer. Of this first element I'm trying to return the image substructure within the Individiual.
c pointers return structure genetic-algorithm
c pointers return structure genetic-algorithm
asked Nov 25 '18 at 7:58
Akila KavisingheAkila Kavisinghe
133
133
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
1
Individual
is astruct
with a componentPPM_IMAGE image;
,indPointer
a pointer toIndividual
. So, when you doindPointer->image
you get an expression of typePPM_IMAGE
. You need an expression ofPPM_IMAGE*
. To get the proper type, you had to doreturn &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.
– Scheff
Nov 25 '18 at 8:04
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an&
:return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.
– Jonathan Leffler
Nov 25 '18 at 8:14
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12
add a comment |
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
1
Individual
is astruct
with a componentPPM_IMAGE image;
,indPointer
a pointer toIndividual
. So, when you doindPointer->image
you get an expression of typePPM_IMAGE
. You need an expression ofPPM_IMAGE*
. To get the proper type, you had to doreturn &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.
– Scheff
Nov 25 '18 at 8:04
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an&
:return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.
– Jonathan Leffler
Nov 25 '18 at 8:14
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
1
1
Individual
is a struct
with a component PPM_IMAGE image;
, indPointer
a pointer to Individual
. So, when you do indPointer->image
you get an expression of type PPM_IMAGE
. You need an expression of PPM_IMAGE*
. To get the proper type, you had to do return &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.– Scheff
Nov 25 '18 at 8:04
Individual
is a struct
with a component PPM_IMAGE image;
, indPointer
a pointer to Individual
. So, when you do indPointer->image
you get an expression of type PPM_IMAGE
. You need an expression of PPM_IMAGE*
. To get the proper type, you had to do return &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.– Scheff
Nov 25 '18 at 8:04
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an
&
: return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.– Jonathan Leffler
Nov 25 '18 at 8:14
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an
&
: return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.– Jonathan Leffler
Nov 25 '18 at 8:14
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12
add a comment |
0
active
oldest
votes
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%2f53465666%2freturning-incorrect-data-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53465666%2freturning-incorrect-data-type%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
Note that generate_population is written as: Individual *generate_population(ARGUMENTS);
– Akila Kavisinghe
Nov 25 '18 at 8:03
1
Individual
is astruct
with a componentPPM_IMAGE image;
,indPointer
a pointer toIndividual
. So, when you doindPointer->image
you get an expression of typePPM_IMAGE
. You need an expression ofPPM_IMAGE*
. To get the proper type, you had to doreturn &indPointer->image;
. What I'm concerned about are the proper life-times of variables but this cannot be justiced by the code fragments you exposed.– Scheff
Nov 25 '18 at 8:04
The function signature says the function returns a pointer to a structure; your return statement apparently tries to return a structure, not a pointer to it. You might be able to simply add an
&
:return &indPointer->image;
if the storage pointed at lasts longer than the function does. Otherwise, you have to work harder, or change the signature to return a structure instead of a pointer.– Jonathan Leffler
Nov 25 '18 at 8:14
The 1st and the last statement of your question are contradicting each other: "returning a pointer to a substructure" vs. "return the [...] substructure" Decide.
– alk
Nov 25 '18 at 16:12