Hi,
1) Rob said:
*test is not a string. test is a string
Youre absolteuly right in this. Sorry, I was in a haste.
2) Rob said:
*test is of type char and value 'a' and should work fine
Well , what I said , and I repeating it now , is :
Take a little program like this:
int main()
{
char* test = "ab";
(*test)++; printf("finished\n");
}Compiling it with gcc on RefHat Linux an running it gives a segmentation fault.
On the other hand,if instead (*test)++ you'll write char c = *test; c++;
It will not crash.
Regards, Amir
From: Robert Collins <[EMAIL PROTECTED]> To: Amir Binyamini <[EMAIL PROTECTED]> CC: [EMAIL PROTECTED], [email protected] Subject: RE: [SLUG] GCC question Date: Tue, 19 Apr 2005 15:39:47 +1000
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>. << signature.asc >>
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
-- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
