• Support PF! Buy your school textbooks, materials and every day products Here!

Assignment makes pointer from integer without cast

  • Thread starter olikimah
  • Start date
  • #1
2
0
"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);
	
	printf("\nAdd another? (y=1/n=0)");
	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);
	
		printf("\nAdd another? (y=1/n=0)");
		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:

Answers and Replies

  • #2
297
2


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:

Related Threads on Assignment makes pointer from integer without cast

Replies
13
Views
25K
Replies
8
Views
3K
Replies
1
Views
1K
  • Last Post
Replies
1
Views
4K
Replies
2
Views
3K
  • Last Post
Replies
9
Views
914
  • Last Post
Replies
1
Views
2K
Top