Thread: [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 19:16:51
|
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]);
|
|
From: Per W. <pw...@ia...> - 2003-09-17 19:24:54
|
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
>
|
|
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
>
|
|
From: Per W. <pw...@ia...> - 2003-09-17 22:25:34
|
Correct.
/Per W
On Wed, 17 Sep 2003, Esteban Valverde wrote:
> 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
> >
>
>
>
>
> -------------------------------------------------------
> 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
>
|
|
From: P.D. v. Z. <pa...@my...> - 2003-09-17 22:37:50
|
Right, now the question has been answered, I just have to say something.
I'd like an array of pints as well :D
Alright, not funny... :)
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Per
Westermark
Sent: Thursday, September 18, 2003 12:24 AM
To: Esteban Valverde
Cc: dev-cpp
Subject: Re: [Dev-C++] Problems with arrays of pinters
Correct.
/Per W
On Wed, 17 Sep 2003, Esteban Valverde wrote:
> 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
> >
>
>
>
>
> -------------------------------------------------------
> 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
>
-------------------------------------------------------
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
|
|
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
|