Question:
What do these following warnings and errors mean that the C compiler is giving me?
2009-04-26 11:48:41 UTC
COMPILER: gcc
OS: UNIX

PROGRAM:
#include
#include

int main(void)
{
int *pointer = malloc(sizeof(*pointer));
printf("%d\n", pointer);

int *pointer2 = malloc(sizeof(*pointer2));
printf("%d\n", pointer2);

int *pointer3 = malloc(sizeof(*pointer3));
printf("%d\n", pointer3);

int *pointer4 = malloc(sizeof(*pointer4));
printf("%d\n", pointer4);

return 0;
}

OUTPUT:
malloc2.c: In function `main':
malloc2.c:7: warning: int format, pointer arg (arg 2)
malloc2.c:9: warning: ISO C90 forbids mixed declarations and code
malloc2.c:10: warning: int format, pointer arg (arg 2)
malloc2.c:12: warning: ISO C90 forbids mixed declarations and code
malloc2.c:13: warning: int format, pointer arg (arg 2)
malloc2.c:15: warning: ISO C90 forbids mixed declarations and code
malloc2.c:16: warning: int format, pointer arg (arg 2)
133568
133584
133600
133616

Thank you for your help. Have a nice day.
Three answers:
2009-04-26 12:09:37 UTC
First, it's yelling at you for trying to print pointers. It'll work, of course, but you probably want to cast it to unsigned int first, especially for portability.



Second, it doesn't want you to make declarations in the code, but instead at the beginning of the main() function. I think if you change the standard to C99 or GNU C99, it won't yell at you, but either way it will work. What kind of UNIX are you using?
2009-04-26 12:14:51 UTC
The only warnings I got from running:



gcc -Wall foo.c



were multiple instances of this:



foo.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’



which is true, you are telling it to print pointers to ints as if they were ints. To fix this, cast all your pointers to ints to something like this.



printf("%d\n", (int)pointer4);



then I got no warnings.
?
2016-12-05 23:23:22 UTC
hey there, I spoke back your previous question, it is basically an area, winmain is touching on the main() function, you have an area between 'important' and '()' so it is giving an errors, try removing the area and compiling it.


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