What wrong with this simple function?

H

howa

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;
str = str[len-i-1];
str[len-i-1] = tmp;
}
}

str can be reversed....
 
H

Howard

howa said:
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;
str = 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
 
D

Daniel T.

howa said:
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;
str = 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?
 
H

howa

// 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;
str = 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 said:
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;
str = 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?
 
X

xhy_China

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

howa

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?
 
J

Jim Langston

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.
 
K

Kai-Uwe Bux

howa said:
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
 
H

howa

Kai-Uwe Bux 寫é“:
howa said:
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.
 
K

Kai-Uwe Bux

howa said:
Kai-Uwe Bux ???
howa said:
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
 
H

howa

Kai-Uwe Bux 寫é“:
howa said:
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

so can i say that

char *str = "apple"; is the same as char const *str = "apple";

as the value can't be modified anyway?
 
K

Kai-Uwe Bux

howa wrote:
[snip]
so can i say that

char *str = "apple"; is the same as char const *str = "apple";

as the value can't be modified anyway?

The difference is that with

char const * str = "apple";

the compiler will barf at an attempt to modifying str. With

char * str = "apple";

an attempt to modify str might go undetected but it will be undefined
behavior.


Best

Kai-Uwe Bux
 
O

Old Wolf

howa said:
void reverse_string(char *str) {

if (str == NULL)
return;

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

Worse than useless cast. Without the cast, your code works for
all strings up to SIZE_MAX in length. With the cast, your code
only works for strings up to INT_MAX in length.
 
H

howa

Kai-Uwe Bux 寫é“:
howa wrote:
[snip]
so can i say that

char *str = "apple"; is the same as char const *str = "apple";

as the value can't be modified anyway?

The difference is that with

char const * str = "apple";

the compiler will barf at an attempt to modifying str. With

char * str = "apple";

an attempt to modify str might go undetected but it will be undefined
behavior.


Best

Kai-Uwe Bux

so is that mean

char * str = "apple";

is useless,

we should consider use char const * str = "apple";
 
M

Marcus Kwok

howa said:
Kai-Uwe Bux ?????????

so is that mean

char * str = "apple";

is useless,

Maybe not useless, but dangerous and not recommended. Basically the
only reason this is allowed is to be able to interface with legacy
functions that are not const-correct. You should only be using this if
you are 100% absolutely sure that the function you are passing it to
does not attempt to modify the string.
we should consider use char const * str = "apple";

This is better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top