Newbie: warning - how can I fix it?

Z

Zbigniew B.

Hallo,

here's a couple of lines from a function:

fname(open)
{
*pwd, *err_log;
char *ctime();
long time(), now;
FILE *fp;

err_log = getenv("ERROR_LOG");

[...never mind the rest...]

The code does compile and works (Linux, gcc) - but everytime after compilation
I've got an annoying warning: "warning: assignment makes pointer from integer
without a cast" - pointing at the line containing the assignment
"err_log = getenv("ERROR_LOG");".

I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?
 
A

Alan Balmer

Hallo,

here's a couple of lines from a function:

fname(open) What type is 'open'?
{
*pwd, *err_log;

What type are these supposed to be?
char *ctime();
long time(), now;
FILE *fp;

err_log = getenv("ERROR_LOG");

There's no prototype for getenv. You need to #include stdlib.h
[...never mind the rest...]

The code does compile and works (Linux, gcc) - but everytime after compilation
I've got an annoying warning: "warning: assignment makes pointer from integer
without a cast" - pointing at the line containing the assignment
"err_log = getenv("ERROR_LOG");".

I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

After correcting this, I suggest you post some more of your code - I
suspect it could use a review.
 
B

Ben Pfaff

Zbigniew B. said:
fname(open)
{
*pwd, *err_log;

`char *' not just `*' surely?
err_log = getenv("ERROR_LOG");

The code does compile and works (Linux, gcc) - but everytime after compilation
I've got an annoying warning: "warning: assignment makes pointer from integer
without a cast" - pointing at the line containing the assignment
"err_log = getenv("ERROR_LOG");".

so GCC assumed that said:
I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

Use -Wall with GCC. It'll warn you about calling undeclared
functions. This tells you to add an appropriate #include.
 
D

Dave Vandervies

Hallo,

here's a couple of lines from a function:
[snip]
[char] *err_log; [snip]
err_log = getenv("ERROR_LOG");
The code does compile and works (Linux, gcc) - but everytime after compilation
I've got an annoying warning: "warning: assignment makes pointer from integer
without a cast" - pointing at the line containing the assignment
"err_log = getenv("ERROR_LOG");".

I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

Did you #include <stdlib.h>?

If you don't provide a proper declaration for a function (f'rexample, by
including the appropriate standard header), it's assumed to return int.
If it does not in fact return an int (as in this case), it's only an
unfortunate accident that it works at all.
(On an x86 architecture, interchanging pointers and integers freely tends
to work as expected, but it's perfectly valid (and, on some architectures,
expected) for pointers and integers to be returned differently; in that
case, code such as this would retreive a garbage value from the "int
return" space and attempt to convert it into a pointer (because that's
what you're assigning it to), which would most likely result in a segfault
(or the moral equivalent of such) as soon as you tried to use the
pointer.)


If this is indeed the case, the compiler should have warned you that it's
using an implicit declaration of the function; the preferred invocation
of gcc is:
gcc -W -Wall -ansi -pedantic -O [other flags, such as -o or -c] [filename]
which turns on this and a lot of other useful warnings. (Some prefer
to omit -W, which turns on even more sometimes-annoying-but-often-useful
warnings.)


dave
 
E

Eric Sosman

Zbigniew said:
Hallo,

here's a couple of lines from a function:

fname(open)
{
*pwd, *err_log;
char *ctime();
long time(), now;
FILE *fp;

err_log = getenv("ERROR_LOG");

[...never mind the rest...]

The code does compile and works (Linux, gcc) - but everytime after compilation
I've got an annoying warning: "warning: assignment makes pointer from integer
without a cast" - pointing at the line containing the assignment
"err_log = getenv("ERROR_LOG");".

I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

Stop trying to outsmart your C implementation.

The standard library functions are all declared in
standard headers: ctime() and time() are in <time.h>,
getenv() is in <stdlib.h>. If you want to use the
functions, you should #include their headers. Do not
attempt to write your own declarations for standard
functions, because it's hard to get them right -- for
example, your home-made declaration of time() is wrong.
(You may not think so, but it is. You are "new to C"
but I am not; just trust me until some of your newness
wears off, all right?)

You appear to be using gcc, which is capable of
warning you when you call an undeclared function (the
direct cause of the message you're getting) -- but only
if you ask it to. Try using the "-Wall -W" command-line
options, or even "-Wall -W -ansi -pedantic" for code that
doesn't require system-dependent features. You can avoid
a lot of trouble by paying attention to gcc's advice ...
 
P

Pedro Graca

Zbigniew said:
here's a couple of lines from a function:

fname(open)
{
*pwd, *err_log;

$ cat t.c
fname(open)
{
*pwd, *err_log;
}


$ gcc --version
gcc (GCC) 3.3.5 (Debian 1:3.3.5-2)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ gcc -W -Wall -ansi -pedantic -c t.c
t.c:2: warning: return type defaults to `int'
t.c: In function `fname':
t.c:2: warning: type of `open' defaults to `int'
t.c:3: error: `pwd' undeclared (first use in this function)
t.c:3: error: (Each undeclared identifier is reported only once
t.c:3: error: for each function it appears in.)
t.c:3: error: `err_log' undeclared (first use in this function)
t.c:3: warning: left-hand operand of comma expression has no effect
t.c:3: warning: statement with no effect
t.c:2: warning: unused parameter `open'
The code does compile and works (Linux, gcc)

It doesn't with my gcc.
I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

Try compiling with '-Wall' to enable all warnings, then get rid of them.
 
T

Trent Buck

Quoth Pedro Graca on or about 2004-11-09:
It doesn't with my gcc.

Looks like the OP quoting his code from memory, instead of cutting and
pasting -- the missing `char' is a bit of a giveaway. I see that a lot
at Uni.

Hint for newbies: Make sure you post your code VERBATIM!

-trent
 
D

Dan Pop

In said:
I'm new to C - I'm not sure, what's the problem. Could someone shortly
explain - and make a tip, how to avoid this?

Being new to C, stop messing with other people's (badly written) code.
Keep reading your C book and solving the exercises within.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top