Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What wrong with this simple function?

Reply
Thread Tools

What wrong with this simple function?

 
 
howa
Guest
Posts: n/a
 
      10-14-2006
void reverse_string(char *str) {

if (str == NULL)
return;

char tmp;
size_t len = strlen(str);
size_t mid = (int) len / 2;

for (size_t i = 0; i < mid; i++) {

tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}
}

str can be reversed....

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      10-14-2006

"howa" <> wrote in message
news: oups.com...
> void reverse_string(char *str) {
>
> if (str == NULL)
> return;
>
> char tmp;
> size_t len = strlen(str);
> size_t mid = (int) len / 2;
>
> for (size_t i = 0; i < mid; i++) {
>
> tmp = str[i];
> str[i] = str[len-i-1];
> str[len-i-1] = tmp;
> }
> }
>
> str can be reversed....
>


How about you tell US what's wrong with it? Maybe we can tell you WHY.
Does it compile? Does it crash? Does it work improperly? In what way?

It helps if you tell us what problem you're having...

-Howard




 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-14-2006
"howa" <> wrote:

> void reverse_string(char *str) {
>
> if (str == NULL)
> return;
>
> char tmp;
> size_t len = strlen(str);
> size_t mid = (int) len / 2;
>
> for (size_t i = 0; i < mid; i++) {
>
> tmp = str[i];
> str[i] = str[len-i-1];
> str[len-i-1] = tmp;
> }
> }
>
> str can be reversed....


Why do you think something is wrong with it? What output were you
expecting, and what output did you get?

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longerÊhave anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
howa
Guest
Posts: n/a
 
      10-15-2006
// full listing

#include <iostream>

using namespace std;

void reverse_string(char *str) {

if (str == NULL)
return;


char tmp;
size_t len = strlen(str);
size_t mid = (int) len / 2;


for (size_t i = 0; i < mid; i++) {


tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}



}

int main() {

char *str = "apple";

reverse_string(str);

cout<<str;

}



// the program return 'apple', not 'elppa'
Daniel T. 寫é“:

> "howa" <> wrote:
>
> > void reverse_string(char *str) {
> >
> > if (str == NULL)
> > return;
> >
> > char tmp;
> > size_t len = strlen(str);
> > size_t mid = (int) len / 2;
> >
> > for (size_t i = 0; i < mid; i++) {
> >
> > tmp = str[i];
> > str[i] = str[len-i-1];
> > str[len-i-1] = tmp;
> > }
> > }
> >
> > str can be reversed....

>
> Why do you think something is wrong with it? What output were you
> expecting, and what output did you get?
>
> --
> There are two things that simply cannot be doubted, logic and perception.
> Doubt those, and you no longerÊhave anyone to discuss your doubts with,
> nor any ability to discuss them.


 
Reply With Quote
 
xhy_China
Guest
Posts: n/a
 
      10-15-2006
you can replace the following sentence
char *str = "apple";
by char str[]=""apple;

 
Reply With Quote
 
howa
Guest
Posts: n/a
 
      10-15-2006

xhy_China 寫é“:

> you can replace the following sentence
> char *str = "apple";
> by char str[]=""apple;


that great! thanks....

btw, what is the difference ?

char *str = "apple";
char str[] = "apple";

both `str` are pointer to the beginning to the string, isn't?

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-15-2006
"howa" <> wrote in message
news: oups.com...

xhy_China ??:

>> you can replace the following sentence
>> char *str = "apple";
>> by char str[]=""apple;

>
>that great! thanks....
>
>.btw, what is the difference ?
>
>.char *str = "apple";


Here str is a char pointer pointing to a *constant* string

>char str[] = "apple";


Here str is a char array initialized to the string "apple"

>both `str` are pointer to the beginning to the string, isn't?


Notice, one is constant, one isn't.


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-15-2006
howa wrote:

>
> xhy_China ???
>
>> you can replace the following sentence
>> char *str = "apple";
>> by char str[]=""apple;

>
> that great! thanks....
>
> btw, what is the difference ?
>
> char *str = "apple";
> char str[] = "apple";
>
> both `str` are pointer to the beginning to the string, isn't?


Nope: the first is a char* that points to the first character of a string
literal (which is constant and that causes the UB you experienced). The
second is an array of char that has been initialized to contain

{ 'a', 'p', 'p', 'l', 'e', 0 }

This array is not declared const and you can modify the contents (although
you cannot change its address or length). The two critters are of utterly
different type and assignments only work one way:

#include <iostream>

int main ( void ) {
char * c_ptr = 0;
char c_arr [] = "apple";
c_arr = c_ptr; // fails to compile!
c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
std::cout << c_ptr << '\n';
}



Best

Kai-Uwe Bux
 
Reply With Quote
 
howa
Guest
Posts: n/a
 
      10-15-2006

Kai-Uwe Bux 寫é“:

> howa wrote:
>
> >
> > xhy_China ???
> >
> >> you can replace the following sentence
> >> char *str = "apple";
> >> by char str[]=""apple;

> >
> > that great! thanks....
> >
> > btw, what is the difference ?
> >
> > char *str = "apple";
> > char str[] = "apple";
> >
> > both `str` are pointer to the beginning to the string, isn't?

>
> Nope: the first is a char* that points to the first character of a string
> literal (which is constant and that causes the UB you experienced). The
> second is an array of char that has been initialized to contain
>
> { 'a', 'p', 'p', 'l', 'e', 0 }
>
> This array is not declared const and you can modify the contents (although
> you cannot change its address or length). The two critters are of utterly
> different type and assignments only work one way:
>
> #include <iostream>
>
> int main ( void ) {
> char * c_ptr = 0;
> char c_arr [] = "apple";
> c_arr = c_ptr; // fails to compile!
> c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
> std::cout << c_ptr << '\n';
> }
>
>
>
> Best
>
> Kai-Uwe Bux


so is that mean

1. if i do not want to modify the value

char *str = "apple" or char str[] = "apple"

the usage dose not matter, the pointer can be used in the same way

2. if i want to modify the value, i need to use char[]

thanks.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-15-2006
howa wrote:

>
> Kai-Uwe Bux ???
>
>> howa wrote:
>>
>> >
>> > xhy_China ???
>> >
>> >> you can replace the following sentence
>> >> char *str = "apple";
>> >> by char str[]=""apple;
>> >
>> > that great! thanks....
>> >
>> > btw, what is the difference ?
>> >
>> > char *str = "apple";
>> > char str[] = "apple";
>> >
>> > both `str` are pointer to the beginning to the string, isn't?

>>
>> Nope: the first is a char* that points to the first character of a string
>> literal (which is constant and that causes the UB you experienced). The
>> second is an array of char that has been initialized to contain
>>
>> { 'a', 'p', 'p', 'l', 'e', 0 }
>>
>> This array is not declared const and you can modify the contents
>> (although you cannot change its address or length). The two critters are
>> of utterly different type and assignments only work one way:
>>
>> #include <iostream>
>>
>> int main ( void ) {
>> char * c_ptr = 0;
>> char c_arr [] = "apple";
>> c_arr = c_ptr; // fails to compile!
>> c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
>> std::cout << c_ptr << '\n';
>> }
>>
>>
>>
>> Best
>>
>> Kai-Uwe Bux

>
> so is that mean
>
> 1. if i do not want to modify the value
>
> char *str = "apple" or char str[] = "apple"
>
> the usage dose not matter, the pointer can be used in the same way


If you do not want to modify, you should use const:

char const * c_ptr = "apple";
char const c_arr [] = "apple";


> 2. if i want to modify the value, i need to use char[]


If you want to modify, you should use std::string.


Actually, you should use std::string anyway:

std::string const banner = "apple";
std::string buffer = "apple";

Is there a reason that you want to make your life harder and your code less
efficient by using char* or char[]?


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple program with a simple(?) error -- what's wrong? RichardOnRails Ruby 3 07-21-2008 01:26 PM
Simple Code? What's Wrong? Peter van der Goes Java 4 07-14-2005 07:15 PM
simple iframe, calling with innerHTML - what am i doing wrong here? Ragnorack67@hotmail.com HTML 9 03-04-2005 07:27 AM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
Simple SQL question. Can you check what am i doing wrong? Thank You. Miguel Dias Moura ASP .Net 1 06-18-2004 09:33 AM



Advertisments