On Mon, 2005-04-18 at 14:53 +0300, Amir Binyamini wrote:
> Hi,
> 
> The reason is that you are trying to incremnt a string
> and not a char; and the incremetn operation is not allowed.
> 
> If you will try to run a simple 2 line program like the following,
> you will get a segmentation fault:
> 
>       char* test = "ab";
>       (*test)++;
> 
> because *test is a string;

huh? *test is not a string. test is a string, *test is of type char and
value 'a'. 'a'++ is valid here and should work fine = test will be on
the stack.
...
> I assume what you meant to do , which works OK with gcc on linux , is:
> 
> void    code(char *);
> main()
>       {
>       code("This is a test string");
>       putchar('\n');
>       return 0;
>       }
> 
> 
> void code (char * strptr)
>     {
>     while( *strptr)
>         {
>         printf("%c", *strptr);
>         ++strptr;
>         }
>     }

This would be somewhat shorter and also more correct as
void
code (char const * strptr)
{
  while (*strptr)
      printf ("%c", *strptr++);
}

Rob
-- 
GPG key available at: <http://www.robertcollins.net/keys.txt>.

Attachment: 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

Reply via email to