TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Herman Schonfeld
from: Kurt Kuzba
date: 1998-08-15 11:52:12
subject: global pointers

HS> CARP.C
HS> ----------------------------------------------
HS> #include 
HS> #include 
HS> #include 
HS> char *text;
HS> int main()
HS> {
HS>    FILE *fp = fopen("CARP.C", "r+");
HS>    fgets(text, 100, fp);
HS>    return 0;
HS> }
HS> // this fails to run.
HS> // If i declare `text' inside main() it does work.
HS> // why does this happen?
HS> // Is there something about globally declared pointer
HS> // that make them function differently from internally
HS> // declared pointers?
   I can't find any reference to it in the standard, which does
   does not mean it does not exist, given my limited talent for
   research, but it would seem that your global variables are
   given static duration and thus static initialization to zero,
   which may prevent your code from operating since it can now
   realize that text is a NULL pointer, with no memory having
   been allocated to it prior to usage. In a Borland app, with
   all the warnings turned on, you would get a warning about a
   possible use before assignment, of the pointer, text.
text = malloc(100);
   Adding the above line prior to the fgets() statement should
   make everything OK again.

> ] I am Elvis of Borg. Thank you... Thank you very much........

---
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 154/750 222 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.