Assignment makes pointer from integer without cast

In summary, the user is getting an error message "assignment makes pointer from integer without cast" when trying to compile their program. This error is likely caused by not including the <stdlib.h> library, which causes the program to think that calloc() returns an integer instead of a pointer. It is recommended to include <stdlib.h> to avoid this error. Additionally, the user may have a mistake on the line where they store the results of the calloc() function. They may be storing the result of the == comparison instead of the returned pointer.
  • #1
olikimah
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:
Physics news on Phys.org
  • #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 to Assignment makes pointer from integer without cast

1. What does "Assignment makes pointer from integer without cast" mean?

"Assignment makes pointer from integer without cast" is an error message that typically occurs in programming languages like C or C++. It means that the code is trying to assign an integer value to a pointer variable without explicitly converting the integer to a pointer type.

2. Why does "Assignment makes pointer from integer without cast" error occur?

This error occurs because pointers and integers are two different data types and require explicit conversion to be assigned to each other. If the conversion is not done, the compiler will throw an error to prevent potential bugs or unintended behavior in the code.

3. How can I fix the "Assignment makes pointer from integer without cast" error?

To fix this error, you need to explicitly convert the integer value to the appropriate pointer type using a typecast. For example, if you have an integer variable "num" and a pointer variable "ptr", you can fix the error by assigning the value as "ptr = (int*) num;".

4. Can I ignore the "Assignment makes pointer from integer without cast" error?

No, it is not recommended to ignore this error. While the code may still compile and run, it can lead to unexpected behavior or even crashes at runtime. It is important to fix this error to ensure the code runs correctly and to avoid potential bugs.

5. Is "Assignment makes pointer from integer without cast" error specific to C and C++ languages?

No, this error can occur in other languages as well, such as Java and Python, which also have specific data types for pointers and integers. However, the error message may be different in these languages as they use different syntax and terminology.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
695
  • Engineering and Comp Sci Homework Help
Replies
3
Views
917
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
974
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top