From: "Edmund Horner" Newsgroups: comp.os.msdos.djgpp References: <8qi4hm$ism$1 AT nnrp1 DOT deja DOT com> Subject: Re: GPF caused by string functions... Lines: 68 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Organization: Paradise Net Message-ID: <969715719.727190@shelley.paradise.net.nz> Cache-Post-Path: shelley.paradise.net.nz!unknown AT 203-79-93-177 DOT tnt11 DOT paradise DOT net DOT nz X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) Date: Sun, 24 Sep 2000 01:31:22 +1200 NNTP-Posting-Host: 203.96.152.26 X-Complaints-To: newsadmin AT xtra DOT co DOT nz X-Trace: news.xtra.co.nz 969715720 203.96.152.26 (Sun, 24 Sep 2000 01:28:40 NZST) NNTP-Posting-Date: Sun, 24 Sep 2000 01:28:40 NZST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com No offense, but I think you should rewrite the function... Otherwise, I believe your GPF will be occuring in the first strcat() line. That's because you don't ensure that buf is a valid string! To fix it, stick this just after the memset(): buf[j] = 0; Try that. (You may have further problems, I haven't checked.) Edmund. wrote in message news:8qi4hm$ism$1 AT nnrp1 DOT deja DOT com... > I am trying to get some string functions to work as part of the > interface for my program. I am using ScreenPutString to write to the > screen and my code appears to work when I call my centrestring() > function once it works okay. A second call, however, causes a General > Protection fault no matter how I specify my parameters. The code that I > am using is: > > /* Include standard DJGPP libraries */ > #include > #include > #include > > /* putstring ( str, fg, bg, col, row ) > Calls the DJGPP function ScreenPutString from libc.a */ > int putstring ( char *str, int fg, int bg, int col, int row ) { > ScreenPutString(str, (bg<<4) | fg, col, row); > } > > /* centrestring ( str, fg, bg, row, col, width ) > Writes the specified string to the screen, centring it as it does */ > int centrestring ( char *str, int fg, int bg, int col, int row, int > width ) { > char buf[81], spacer[81]; > int i,j,k; > i = strlen(str); > j = (width - i) / 2; > memset(buf,' ',--j); > strcat(buf,str); > memset(spacer,' ',(width-i-j)); > strcat(buf,spacer); > buf[width] = 0; > putstring(buf,fg,bg,col,row); > } > > /* main > Tests the code */ > int main ( void ) { > centrestring("Just one line is okay",WHITE,BLUE,0,0,80); > centrestring("This should work.",WHITE,BLUE,0,1,80); > } > > If anyone has any ideas as to why this doesn't work I would appreciate > any feedback. > > Regards, > > Ed > > > Sent via Deja.com http://www.deja.com/ > Before you buy.