Mail Archives: djgpp/2000/09/23/09:30:10
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.
<raynee AT my-deja DOT com> 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 <pc.h>
> #include <conio.h>
> #include <string.h>
>
> /* 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.
- Raw text -