🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.msdos.programmer
Article #1159

Re: Pascal program works but not C program!

#1159
From: Kohn Emil Dan
Date: Thu, 04 Jan 1996 10:18
97 lines
2873 bytes

On 3 Jan 1996, Gregary John Boyles wrote:

> EMail : boyles@lux.latrobe.edu.au
>       
> Why does the Pascal version of this program behave exactly as expected while the
> C++ version does not?
> 
> 
> 
> 
> ******************************* PASCAL VERSION *******************************
> 
/*Pascal  code removed*/ 
> 
> ******************************** C++ VERSION *********************************
> 
> #include <stdio.h>
> #include <conio.h>
> #include <string.h>
> #include <alloc.h>
> #include <process.h>
> 
> const maxstring=127;
> const lineend='\n';
> const escape=27;
> const null='\0';
> const space=' ';
> const up='H';
> const down='P';
> const pageup='I';
> const pagedown='Q';
> const left='K';
> const right='M';
> const home='G';
> const end_='O';
> 
> 
> 
> void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
> {
>    char y;
> 
>    window(1,1,width,height);
>    clrscr();
>    window(1,1,width,height+1);
>    y=1;
>    while ((y<=height) && (!empty(posptr->next)))
>    {
>         gotoxy(1,y);
>         strcpy(line_,posptr->line);
>         line__=&(line_[stringpos]);
>         strncpy(line,line__,width-1);
>         strcat(line,null);
                       ^---------------This is a problem!!!!!!!!!
                       
>         cputs(line);
>         posptr=posptr->next;
>         y++;
>    }
> }
> 
> 
> void main(int argc,char *argv[])
   ^-----------------------------This one too, but not too serious
> {
> }
> 
> 


I hadn't read  your code thoroughly, but I think your problem is with 
strcat (I mean surely there is a problem with the usage of strcat, but I 
don't know if it's the only one). In C (and in C++ too), a string of length 
1 is _NOT_ the same thing as a character. So null (as defined by you) is _NOT_ a string.
An empty string is defined as "". If you want to append a '\0' string 
terminator to line_  (I think this was your intention), use the statement:

line_[width-1]='\0'; (or using your constant line_[width-1]=null;) 

Don't bother, if the length of line__ is less than width-1; strncpy padds 
the destination string with '\0'-s.



Another problem, not as serious as the previous one, although this is not
the reason the C++ version might not work; main() should be defined
returning int, not void. 


Hope this helps.


					Best regards, 

								Emil
Technical Details
Message-ID: <Pine.SV4.3.91-heb-2.04.960104114930.16292A-100000@cs.technion.ac.il>
Path: arc1-ca.pugleaf.net!news.kei.com!wang!news
References: <4cdpr2$psi@lugb.latrobe.edu.au>

Show Full Headers
Complete Article Headers
Newsgroups: alt.msdos.programmer,comp.lang.c
Path: arc1-ca.pugleaf.net!news.kei.com!wang!news
From: emild@cs.technion.ac.il (Kohn Emil Dan)
Subject: Re: Pascal program works but not C program! 
Organization: Technion, Israel Institute of Technology
Date: Thu, 4 Jan 1996 10:18:15 GMT
Message-ID: <Pine.SV4.3.91-heb-2.04.960104114930.16292A-100000@cs.technion.ac.il>
References: <4cdpr2$psi@lugb.latrobe.edu.au> 
Sender: news@wang.com