Click to See Complete Forum and Search --> : Odd warning message in gcc when stdlib.h is not included with method malloc...
YourSurrogateGod
October 8th, 2004, 09:35 PM
The program in question is below. And the boldened line below is where I get the odd warning. The problem is when I uncomment "#include <stdlib.h>", the boldened line gives a warning saying that "warning: initialization makes pointer from integer without a cast", this warning message can be eliminated by putting a (struct node *) in front of malloc.
My question is why does this happen whenever "#include <stdlib.h>" is commented?#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_node(struct node **, int);
void remove_node(struct node **);
int main()
{
return 0;
}
void insert_node(struct node **head, int value)
{
struct node *new_node = malloc(sizeof(struct node));
new_node -> data = value;
new_node -> next = *head;
(*head) = new_node;
}
void remove_node(struct node **head)
{
}Note: I know that this program is far from finished. It's just that I've noticed something odd and don't know why it's happening :shrug: .
Thanks in advance :wave: .
Latem
October 9th, 2004, 10:10 AM
hmm that is weird, it works exactly as expected for me.
if I copy your code, and compile using line:
gcc -Wall -o mallocc mallocc.c
Everything compiles fine, without any errors, or warnings.
If I comment out the #include <stdlib.h> statements I get warnings:
mallocc.c: In function `insert_node':
mallocc.c:20: warning: implicit declaration of function `malloc'
mallocc.c:20: warning: initialization makes pointer from integer without a cast
which is expected since malloc needs stdlib.h.
http://www.uwo.ca/cgi-bin/rmanx.pl?topic=malloc/section=3C
But it's backwards from what u said u are getting. You say when you uncomment the include statement it gives the error, but when the statement is commented everything is ok.
what version of gcc are you using? and what version of libc?
I have gcc version 3.3.2 and libc 2.3.3, both are fairly recent. I am not sure if you are using windows or Linux. On a Linux system you check GCC version with command
gcc --version
and you check libc version just by going to the library so:
/lib/libc.so.6
for example. that's how it is on my system.
I dont know how to check these things on Windows.
EDIT:
Ok I just caught this:My question is why does this happen whenever "#include <stdlib.h>" is commented?
So I am not sure do u get the warning when u comment the line, or when you uncomment it?
If you get the warnings w/ the include statment commented then everything is ok. That's how it should work.
Latem
YourSurrogateGod
October 9th, 2004, 11:39 AM
hmm that is weird, it works exactly as expected for me.
if I copy your code, and compile using line:
gcc -Wall -o mallocc mallocc.c
Everything compiles fine, without any errors, or warnings.
If I comment out the #include <stdlib.h> statements I get warnings:
mallocc.c: In function `insert_node':
mallocc.c:20: warning: implicit declaration of function `malloc'
mallocc.c:20: warning: initialization makes pointer from integer without a cast
which is expected since malloc needs stdlib.h.
http://www.uwo.ca/cgi-bin/rmanx.pl?topic=malloc/section=3C
But it's backwards from what u said u are getting. You say when you uncomment the include statement it gives the error, but when the statement is commented everything is ok.
what version of gcc are you using? and what version of libc?
I have gcc version 3.3.2 and libc 2.3.3, both are fairly recent. I am not sure if you are using windows or Linux. On a Linux system you check GCC version with command
gcc --version
and you check libc version just by going to the library so:
/lib/libc.so.6
for example. that's how it is on my system.
I dont know how to check these things on Windows.
EDIT:
Ok I just caught this:
So I am not sure do u get the warning when u comment the line, or when you uncomment it?
If you get the warnings w/ the include statment commented then everything is ok. That's how it should work.
LatemThanks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.