 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael Jarrod Guest
|
Posted: Mon Apr 16, 2007 8:19 pm Post subject: Newbie pointers and reversing question |
|
|
hi,
I have the following code piece of code:
#include <stdio.h>
#include <string.h>
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Bröker Guest
|
Posted: Thu Apr 19, 2007 7:14 pm Post subject: Re: Newbie pointers and reversing question |
|
|
Michael Jarrod wrote:
| Quote: | char a_str[8] = "abc def";
a_str[7] = '\0';
|
What did you think you had to go and to _that_ for? Were you actually
worried that a_str[7] might be unequal to '\0'?
[...]
| Quote: | char* b_str = "abc def";
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
|
You went wrong in that "I always though" part. No, they're not
equivalent. The difference is that a_str is an array initialized from a
string literal, whereas b_str is a pointer to constant string. If your
textbooks can't teach you this distinction, trash it and get a proper
one. The FAQ has pointers to some.
From a point-of-view governed by didactic principles or rigorousness,
this way of initializing b_str should probably have been forbidden.
History happened otherwise.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Apr 19, 2007 7:14 pm Post subject: Re: Newbie pointers and reversing question |
|
|
Michael Jarrod wrote:
| Quote: | char a_str[8] = "abc def";
char* b_str = "abc def";
I've always thought that the definitions of a_str and b_str
were more or less equivelent.
|
Most likely, string literals are unmodifiable on your platform.
b_str points to a string literal. a_str is a modifiable array
of element type "char" that is initialized with characters
taken from what looks syntacically like a string literal.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bono Guest
|
Posted: Thu Apr 19, 2007 7:15 pm Post subject: Re: Newbie pointers and reversing question |
|
|
On 16 Apr., 17:19, "Michael Jarrod" <Michael.Jarrod (AT) gmail (DOT) com> wrote:
| Quote: | hi,
I have the following code piece of code:
#include <stdio.h
#include <string.h
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
Hi Michael,
a string literal created by a pointer definition like this, char*
b_str = "abc def"; is defined as read-only in ANSI C.
It is undefined behaviour if you try to change the literal through
b_str. Instead, char a_str[8] = "abc def"; the string literal created
by this array initialization is writeable, thus you are allowed to
change the string through a_str.
For more information on this topic see
http://c-faq.com/decl/strlitinit.html
hth
Flo
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Kevin Ashley Guest
|
Posted: Thu Apr 19, 2007 7:15 pm Post subject: Re: Newbie pointers and reversing question |
|
|
Michael Jarrod wrote:
| Quote: | hi,
I have the following code piece of code:
[snip] |
| Quote: | {
char a_str[8] = "abc def";
a_str[7] = '\0';
.....
char* b_str = "abc def";
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
|
They aren't equivalent. a_str is an array of 8 chars, with
storage allocated locally to main. b_str is a pointer to
char. Storage is allocated only for the pointer.
Think about what would happen if you separated the
definitions of the variables from their initialisations:
char a_str[8];
a_str[0] = 'a'; a_str[1] = 'b' (and so on..)
char *b_str;
b_str = "abd def";
You can't initialise b_str the way you initialised a_str
because there's no storage to put the characters into.
So you've put a pointer to a constant string in there instead.
A *constant* string: whose contents you aren't allowed to modify.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
mahesh Guest
|
Posted: Thu Apr 19, 2007 7:15 pm Post subject: Re: Newbie pointers and reversing question |
|
|
On Apr 16, 8:19 pm, "Michael Jarrod" <Michael.Jar...@gmail.com> wrote:
| Quote: | hi,
I have the following code piece of code:
#include <stdio.h
#include <string.h
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
allocate memory for b_str and try. hope it works
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
tao3256 Guest
|
Posted: Sat Apr 21, 2007 5:15 pm Post subject: Re: Newbie pointers and reversing question |
|
|
mahesh wrote:
| Quote: | On Apr 16, 8:19 pm, "Michael Jarrod" <Michael.Jar...@gmail.com> wrote:
hi,
I have the following code piece of code:
#include <stdio.h
#include <string.h
void reverse(char* begin, char* end)
{
char tmp;
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
++begin;
--end;
}
}
int main(int argc, char* argv[])
{
char a_str[8] = "abc def";
a_str[7] = '\0';
printf("str: %s\n",a_str);
reverse(a_str,a_str + strlen(a_str) - 1);
printf("str: %s\n",a_str);
char* b_str = "abc def";
printf("str: %s\n",b_str);
reverse(b_str,b_str + strlen(b_str) - 1);
printf("str: %s\n",b_str);
return 0;
}
when I use a_str for the parameter to the function reverse
everthing is ok, however when b_str is used the program
crashes when the line containing *begin = *end is executed.
I've always thought that the definitions of a_str and b_str
were more or less equivelent. Can someone explain where I
have gone wrong.
-Mich
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
allocate memory for b_str and try. hope it works
|
Agree.
When you declare a pointer as a c-string (character array), you must
allocate memory for it. In this case, char* b_str does need to allocate
memory for it.
In C++, we do this,
char* b_str;
b_str = new char[8]; // capable of storing up to 7 characters + '\0'
strcpy(b_str, "abc def");
Dr. Boris Chang
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Mon Apr 23, 2007 10:59 pm Post subject: Re: Newbie pointers and reversing question |
|
|
tao3256 <jenny1972 (AT) fastmail (DOT) fm> writes:
| Quote: | mahesh wrote:
On Apr 16, 8:19 pm, "Michael Jarrod" <Michael.Jar...@gmail.com> wrote:
[...]
char a_str[8] = "abc def";
[...]
char* b_str = "abc def";
[...]
allocate memory for b_str and try. hope it works
Agree.
When you declare a pointer as a c-string (character array), you must
allocate memory for it. In this case, char* b_str does need to
allocate memory for it.
|
Pointers, strings, and character arrays are three very different
things. Read section 6 of the comp.lang.c FAQ, <http://c-faq.com>.
| Quote: | In C++, we do this,
char* b_str;
b_str = new char[8]; // capable of storing up to 7 characters + '\0'
strcpy(b_str, "abc def");
|
That would be relevant in comp.lang.c++{,.moderated}.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|