Re: [Dev-C++] Problems with arrays of pinters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Esteban V. <est...@ho...> - 2003-09-17 21:40:03
|
So... char *str2 = "sldfksf" must be undertaken like a read-only string,
whereas char str2[ ] = "sldfksf" is a read-write string ?
----- Original Message -----
From: "Per Westermark" <pw...@ia...>
To: "Esteban Valverde" <est...@ho...>
Cc: "dev-cpp" <dev...@li...>
Sent: Wednesday, 17 September, 2003 16:24 PM
Subject: Re: [Dev-C++] Problems with arrays of pinters
Note that when you declare a text string as
char *str2 = "sldfksf"
str2 is only a pointer to a text. Most compilers will place the text in
read-only memory, i.e. if you try to follow the pointer to change the
text, you will get a memory access violation.
If you want to change a text you must use the array form of declaration.
/Per W
On Wed, 17 Sep 2003, Esteban Valverde wrote:
> Hi, the next code has an running error, non a compiler error. Anybody can
> understand it ?
>
> thanks, Esteban
>
>
> char str1[] = "Hello, how ";
> char *str2 = "are you ?";
>
> char *pstr[2];
>
> // these works fine...
> pstr[0] = str1;
> pstr[1] = str2;
>
> // these works fine...
> printf("%s%s\n", pstr[0], pstr[1]);
>
> // these works fine...
> if(*pstr[0] == 'H')
> printf("\"%s\" start with 'H'\n", pstr[0]);
>
> if(*pstr[1] != 'x')
> printf("\"%s\" don't start with 'X'\n", pstr[1]);
>
> // I'm attempting to increment 'H' to 'I'.
> // these works fine...
> (*pstr[0])++;
> printf("%s\n", pstr[0]);
>
> // I'm attempting to increment 'a' to 'b'.
> // these, doesn't works fine...
> // these halts the computer... the same with (*(pstr[1]))++
> (*pstr[1])++;
> printf("%s\n", pstr[1]);
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|