TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Herman Schonfeld
from: William Mcbrine
date: 1998-08-12 14:56:06
subject: global pointers

-=> Herman Schonfeld wrote to All <=-

 HS> char *text;
 HS> 
 HS> int main()
 HS> {
 HS>    FILE *fp = fopen("CRAP.C", "r+");
 HS>    fgets(text, 100, fp);
 HS>    return 0;
 HS> }

You haven't allocated any memory to *text.

 HS> this fails to run. If i declare `text' inside main() it does work.
 HS> why does this happen?

Beats me -- it shouldn't work then, either. :-) Actually I do have a guess
as to what's going on...

 HS> Is there something about globally declared pointers that make them
 HS> function differently from internally declared pointers? 

If not explicitly initialized, globals get initialized to 0, while locals
are filled with garbage (whatever happened to be in memory). In your case,
that means that the global version of *text is NULL, while the local
version is pointing to some random block of memory that happens to work.

... Ah, Dinner's ready.  The smoke detector just went off!

---
* Origin: * My Place BBS * Bowie, Md USA * V.34 * (301)805-1602 * (1:109/570)
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: 109/570 13/25 270/101 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™.