[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference 7.286::atarist

Title:Atari ST, TT, & Falcon
Notice:Please read note 1.0 and its replies before posting!
Moderator:FUNYET::ANDERSON
Created:Mon Apr 04 1988
Last Modified:Tue May 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1433
Total number of notes:10312

361.0. "getenv() & MWC" by PEARS::BOEHM () Wed Jan 04 1989 00:36

Hi.

I could need some help from a C programmer (MWC!). I have a
problem with the getenv() function. Using this function brings up
a MWC compiler error ("integer pointer pun"), although the left
side of the assignment is declaered as (e.g.) 'char *path;' and
the right side is 'char *getenv(variable) char *variable'
according to the MWC documentation.

I tested the function with a little programm out of the TURBO
C/ST documentation. When I run the (MWC compiled) program, it
comes up immediately with the shell prompt and 2 bombs (a bus
error, I think). The TURBO C compiler doesn't complain about line
10 + 11. The program runs without an error and bombs.

Is this a bug with the MWC compiler??

My best wishes for the new year and regards from Munich.

Hilmar.

--------------------------------------------------------------------------
Jan  4 02:57 1989  tstgtenv.c  Page 1, line 1


   1: /* test getenv() / example from the TURBO-C User's Guide, p. 229 */
   2: #include <stdio.h>
   3: #include <osbind.h>
   4:
   5: main()
   6: {
   7:
   8:     char *path, *dummy;
   9:
  10:     path = getenv("INCDIR");
  11:     dummy = getenv("DUMMY");
  12:     printf("INCDIR = %s\n", path);
  13:     printf("DUMMY = %s\n", (dummy == NULL) ? "(nicht gefunden)" : dummy);
  14: }
------------------------------------------------------------------------------
Mark Williams C for the Atari ST, 3.0.6
Copyright 1984-1988, Mark Williams Co., Chicago
e:\lib\cc0.prg D220000000000000 tstgtenv.c e:\tmp\cc30333b
10: tstgtenv.c: Strict: integer pointer pun
11: tstgtenv.c: Strict: integer pointer pun
e:\lib\cc1.prg D220000000000000 e:\tmp\cc30333b e:\tmp\cc30333a
e:\lib\cc2.prg D220000000000000 e:\tmp\cc30333a tstgtenv.o e:\tmp\cc30333b
rm e:\tmp\cc30333a
rm e:\tmp\cc30333b
e:\bin\ld.prg -X -o tstgtenv.prg e:\lib\crts0.o tstgtenv.o e:\lib\libc.a
rm tstgtenv.o
----------------------------------------------------------------------------
T.RTitleUserPersonal
Name
DateLines
361.1Declare your functions before you use themPRNSYS::LOMICKAJJeff LomickaWed Jan 04 1989 14:3822
Neither STDIO.H nor OSBIND.H declare getenv() for you.  C, by
definition, assumes that any function that has not been previously
declared as being an "extern int" function.  When the documentation
tells you the the function returns "char *", "long" or "float", it
becomes very important that you declare the function before you call it.

What happened in this case is that C took the low order 16 bits of the
"int" return value, sign extended that into a 32-bit pointer, and
(incorrectly) used this value.  It was also kind enough to warn you that
this had happened.

To make your program work correctly, add the line:

extern char *getenv();

near the beginning.


NOTE!  If you use malloc(), calloc(), realloc(), lmalloc(), lrealloc(),
or lcalloc(), you MUST declare these as "extern char *" before you use
them, or you will experience a similar problem.

361.2It works...PEARS::BOEHMFri Jan 06 1989 11:0911
    Hi Jeff,
    
    thank you very much for your fast reply. It helped, the program works
    now. I know this trap of the default function type but didn't think
    of it.
    
    Btw this program is the first C Crossreferencer I got running on the ST
    (it's out of: CSCMAS::SYS$MSDOS:[C]CUG236.ARC - XC2).
    
    Regards. / Hilmar.