TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Herman Schonfeld
from: Bill Birrell
date: 1998-08-13 07:10:00
subject: global pointers

Hey, Herman!

 > #include 
 > #include 
 > #include 


 > char *text;


 > int main()
 > {
 >    FILE *fp = fopen("CRAP.C", "r+");

 >    fgets(text, 100, fp);

 >    return 0;
 > }

 > // this fails to run. If i declare `text' inside
 > main() it does work.
 > // why does this happen? Is there something about
 > globally declared pointers
 > // that make them function differently from internally
 > declared pointers?

    Try dclaring text as char text[101], Herman. Then you will actually
reserve storage for 101 bytes, and not just for the two that the pointer
needs. Alternatively you can allocate 101 bytes to text by using malloc()
(and free() when you've finished with it). Allocate storage space somehow,
though, before you try to fill it up - otherwise the data will be written
at random all over the programm - or wherever text happens to point. :-)

    The primary difference between an array and a pointer *is* automatic
allocation of storage space for data. Arrays have it - pointers don't.

Best wishes,
Bill.

---
* Origin: Meerkats Anonymous (2:2504/200)
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: 2504/200 213 2503/501 252/356 140/1 270/101 396/1 633/260 635/506 728
@PATH: 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™.