Assignment makes pointer from integer without cast

Click For Summary
The error "assignment makes pointer from integer without cast" occurs due to improper handling of the return value from the calloc function, likely because the necessary header file <stdlib.h> is not included. The code attempts to assign a pointer to an integer comparison, which leads to the warning. The problematic line is where the result of calloc is incorrectly checked for NULL instead of assigning it to current->next. This results in a logical error where the pointer is not correctly initialized. Ensuring the correct use of calloc and including the appropriate headers will resolve the issue.
olikimah
Messages
2
Reaction score
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:
Physics 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:

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K