Question:
C Program Warning: initialization makes pointer from integer without a cast?
anonymous
2012-04-15 09:03:19 UTC
[Warning] initialization makes pointer from integer without a cast

When you initialize it to any number here i have written 1
struct student stu={"marry",1};

if you remove that 1
struct student stu={"marry"};
then no warning.
Why
what does it warning mean
[Warning] initialization makes pointer from integer without a cast

#include
struct student{
char name[10];
int *ptrmem;
};

main()
{
struct student stu={"marry",1};
struct student *ptr = &stu;

/*Access value of ptrmem*/
printf("%d\n",stu.ptrmem);
printf("%d\n", ptr->ptrmem);

getch();
}
Three answers:
?
2012-04-15 15:52:01 UTC
Well, "Don't sue me" obviously likes c++ because the answer given is laced with c++. You specifically mention C. So it's not entirely helpful, though still reasonably informed.



Why are you declaring ptrmem to be a pointer? (The * tells C it is and you know that given the "struct student *ptr;" definition you later write and properly initialize.) Do you really want a pointer to an integer there? Or just an integer? If just an integer, I'd recommend renaming your variable (the 'ptr' part of it will be sure to confuse) and getting rid of the *. If you really do want a pointer, then why are you wanting to set it to 1??? Do you know what is at address 1, somehow?



Pointers should be set to proper addresses using appropriate syntax to object instances or to NULL or 0. Unless you are doing hardware work on an embedded platform with memory mapped hardware, there is no reason to simply assign a constant value into a pointer (other than 0.)



Is this structure supposed to be part of a linked list of names, so that ptrmem is really supposed to be a pointer to another structure or to NULL? Can you elaborate a little about what the overall goals are?



If you insist on setting a pointer to 1, for reasons beyond me, then simply cast it as (int *) 1 and the warning will likely go away. Still bizarre. But if that's what you want, and you don't want the warning, that's a way to get at it -- though I've worked with compilers that will give you a different warning if you try and do that.
Don't sue me!
2012-04-15 09:08:19 UTC
You're initializing student.ptrmem which is an int* (pointer to int) with a value of 1, which is an int.



You need to do this:

int ptrmem; // remove the *



or this:

struct student stu={"marry", new int(1)};

....

printf("%d\n",*stu.ptrmem);

printf("%d\n", *ptr->ptrmem);

....

delete stu.ptrmem;
word
2016-10-22 01:44:46 UTC
once you position a parameter in a decision series the techniques would properly be great, hence to avert a lengthy put off, at the same time as passing a whopping massive array, it is smart to bypass a very small aspect truly thats the handle of the array. otherwise ALL courses may bypass heavily sluggish for no sturdy reason in any respect. so its waiting for a POINTER so its waiting for &array


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...