Assignment makes pointer from integer without cast

Click For Summary
SUMMARY

The forum discussion addresses a compilation error in C programming: "assignment makes pointer from integer without cast." The issue arises from incorrect usage of the calloc function in the provided code snippet. Specifically, the line current->next = ((elephant*)calloc(1,sizeof(elephant)) == NULL); mistakenly assigns a boolean result to a pointer variable instead of the allocated memory address. The correct approach involves directly assigning the result of calloc to current->next without the comparison.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of dynamic memory allocation using calloc
  • Pointer manipulation in C
  • Basic knowledge of linked list data structures
NEXT STEPS
  • Review C programming memory management techniques
  • Learn about linked list implementation in C
  • Study error handling in C for dynamic memory allocation
  • Explore debugging techniques for C compilation errors
USEFUL FOR

C programmers, software developers working with dynamic data structures, and anyone troubleshooting memory allocation issues in C code.

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
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · 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
2K
  • · Replies 2 ·
Replies
2
Views
2K