From: "Glynne Casteel" Newsgroups: comp.os.msdos.djgpp Subject: Re: PROBLEM WITH ARRAY IN STRUCT Date: Sat, 28 Mar 1998 22:50:44 -0700 Organization: Netcom Lines: 76 Message-ID: <6fknpm$dmr@dfw-ixnews11.ix.netcom.com> References: <351D93C1 DOT 60A91521 AT mail DOT ucsm DOT edu DOT pe> NNTP-Posting-Host: den-co11-19.ix.netcom.com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk rpinnell AT characato DOT ucsm DOT edu DOT pe wrote in message <351D93C1 DOT 60A91521 AT mail DOT ucsm DOT edu DOT pe>... >Hi I am trying to write a program using DJGPP and the Allegro graphics >library. I am having a problem with an array inside a struct. Here is >example of the type of thing I am doing. > >#include >#include >#include >#include >#include "my.h" > >struct my >{ >char block[336]; >}*pt; > >/* START OF MAIN */ >int main() >{ >int i=0; >DATAFILE *df; >char *buf; > > /* INITIALISE ALLEGRO */ > allegro_init(); > install_keyboard(); > > /* LOAD DATAFILE */ > strcpy(get_filename(buf), "my.dat"); > df = load_datafile(buf); > > /* SET GRAFICS MODE */ > set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0); > set_palette(df[mypal].dat); > > /* FILL BLOCK WITH ZEROS */ > while(i<336) /* THIS SEEMS TO CAUSE LOTS OF PROBS */ > > pt->block[i++]=0; /* WHY? WHY? WHY? */ > >readkey(); >return 0; >} > >The above code does nothing of course it is just to demonstrate, it >compiles ok. The program I am writting is much more complex and >contains multiple files. The problem is the line which writes data to >the array in the struct my.. In my program I am passing a pointer to >this struct to various functions and manipulating the data within the >array. However whenever I try and initialise the array in the struct >with 0īs, as in the above examples I get all sorts of problems. The >problems donīt seem to be consistent, sometimes it works ok when I >compile other times it compiles ok but I get exiting due to signal >SIGSERV. I tried running the program through the rhide debugger and >again the errores seem to be inconsistent, somtimes the error is when >the program loads in the allegro datafile sometimes elsewhere. There >is no problem before I add the loop to initialise the array in the >struct however. Iīm sure I am doing something very very silly here can >someone please tell me what it is. >Richard Pinnell >Arequipa Peru > > > You forgot to allocate your struct pointer, e.g. pt= malloc( sizeof(pt[0]) ); You can either do this, or simply declare pt as a struct instead of a pointer-to-struct. Then use the struct member operator (.) instead of the struct pointer operator (->)