On Sun, 2004-04-18 at 20:58 +1000, David Bowskill wrote:
> HI SLUGgers,
> I have this little problem with GCC which I wonder if anyone can
> enlighten me on. The little program below has been run on three
> different machines all with the same result. The program compiles OK
> under GCC but fails with a segmentation error. Under Borland 'C for
> DOS', it compiles and runs correctly.
>
> Thanks in anticipation
>
> David
>
> /*
> Program to increment the chars in a string
> and print to screen. Works OK with Borland
> 'C for DOS' but fails with GCC 3.3
> (segmentation error) -why????
> */
> #include <stdio.h>
>
> void code(char *);
>
> main()
> { code("This is a test string");
> putchar('\n');
> return 0;
> }
>
> /*
> (*strptr)++ gets then increments the CHARACTER,
> while *strptr++ gets the CHARACTER then
> increments the POINTER
> */
>
> void code (char * strptr)
> {
> while( *strptr)
> { (*strptr)++ ; //This line fails in GCC
> //(*strptr) = (*strptr) + 1 ;// so does this
> printf("%c", *strptr++);
> }
> }
As I read this you want to print out the string with each character
shifted up one in the ascii table. If the string is in non modifiable
memory space - for example the .code section rather than the stack, then
this will fail.
try:
void
code (char const * strptr)
{
while (*strptr)
printf ("%c", *strptr++ + 1);
}
which will not modify the string in place and thus won't fail.
Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.
signature.asc
Description: This is a digitally signed message part
-- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
