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;
aslo If you'll try something a little similiar :
char myTest[] = "ab";
myTest++;you'll get a compilation problem (in gcc)
("wrong type argument to increment")It could be that the borelnad environment is less strict so it does not crash ; BTW - what does it print when running in borland?
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; } }
Regards, Amir
From: David Bowskill <[EMAIL PROTECTED]> To: [email protected] Subject: [SLUG] GCC question Date: Sun, 18 Apr 2004 20:58:56 +1000
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++); } }
-- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/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
