Re: [Dev-C++] Problems with arrays of pinters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-09-17 19:47:17
|
Esteban Valverde wrote:
> Hi, the next code has an running error, non a compiler error. Anybody can
> understand it ?
> char str1[] = "Hello, how ";
> char *str2 = "are you ?";
>
> char *pstr[2];
...
> // 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]);
str2 is a pointer. It points to a constant string: "are you ?"
Constant strings are NOT allowed to be changed, as they are hard coded
into the exe. So the problem lies here, and not in any sort of
dereferencing. However, in the first case, the thing is different, as
you have an array (str1), not a pointer, and you initialise this array
with a string.
Try this, and it will work:
char str3[] = "are you ?";
char str2 = str3;
I hope this helped.
--
Greetings,
Balázs
|