# Assignment makes pointer from integer without cast

"assignment makes pointer from integer without cast"

Code:
elephant*
get_elephants()
{
elephant *current, *first;
int response;

/* create first node */

first = (elephant*)calloc(1,sizeof(elephant));                               /* THIS LINE */

current = first;

printf("Elephant name? ");
scanf ("%s", current->name);

printf("Elephant weight? ");
scanf ("%d", &current->weight);

scanf ("%d", &response);

while (response == 1)
{
current->next = ((elephant*)calloc(1,sizeof(elephant)) == NULL);

current = current->next;

printf("Elephant name? ");
scanf ("%s", current->name);

printf("Elephant weight? ");
scanf ("%d", &current->weight);

scanf ("%d", &response);
}

current->next = NULL;

return (first);
}
When I try to compile the program I get the error message "assignment makes pointer from integer without cast." for the line which is commented.

Could anyone help me with this?

Last edited by a moderator:

Related Engineering and Comp Sci Homework Help News on Phys.org

Did you #include <stdlib.h>?

If not, it probably thinks calloc returns an int.

EDIT: Mentioned this to my friend (who's a programmer) who went on a short rant about how annoying that is, and a nasty bug he had a while ago with that and sin/cos functions. So if you thought "well that's dumb, wouldn't it be better if it wouldn't compile in those cases?", then join the club.

EDIT: On second thought, are you sure that's the line getting that warning? Seems to me this line should give you that warning:

Code:
current->next = ((elephant*)calloc(1,sizeof(elephant)) == NULL);
In which case you're storing the results of the ==, rather than the returned pointer. That won't work.

Last edited: