How modify character arrays inside functions?
I want to update a character array by reversing it. The character array is correct when output inside function, but not in main
after the function is called.
I believe that the character array is passed by reference, but when updating the array, the character array in the main function is not updated. What am I doing wrong here?
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
ch = sh;
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
I do not want to print the result inside the function, but update the character array 'ch' in the main
function by calling the function StringReverse
.
c++ arrays char pass-by-reference pass-by-value
add a comment |
I want to update a character array by reversing it. The character array is correct when output inside function, but not in main
after the function is called.
I believe that the character array is passed by reference, but when updating the array, the character array in the main function is not updated. What am I doing wrong here?
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
ch = sh;
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
I do not want to print the result inside the function, but update the character array 'ch' in the main
function by calling the function StringReverse
.
c++ arrays char pass-by-reference pass-by-value
1
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
2
You want to usestrncpy()
instead ofch = sh;
.
– Joey Mallone
Jan 2 at 10:22
Changing the copy of the pointer inStringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.
– Matthieu Brucher
Jan 2 at 10:22
add a comment |
I want to update a character array by reversing it. The character array is correct when output inside function, but not in main
after the function is called.
I believe that the character array is passed by reference, but when updating the array, the character array in the main function is not updated. What am I doing wrong here?
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
ch = sh;
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
I do not want to print the result inside the function, but update the character array 'ch' in the main
function by calling the function StringReverse
.
c++ arrays char pass-by-reference pass-by-value
I want to update a character array by reversing it. The character array is correct when output inside function, but not in main
after the function is called.
I believe that the character array is passed by reference, but when updating the array, the character array in the main function is not updated. What am I doing wrong here?
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
ch = sh;
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
I do not want to print the result inside the function, but update the character array 'ch' in the main
function by calling the function StringReverse
.
c++ arrays char pass-by-reference pass-by-value
c++ arrays char pass-by-reference pass-by-value
edited Jan 2 at 10:59
Holt
25.8k65195
25.8k65195
asked Jan 2 at 10:17
sparrowsparrow
153
153
1
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
2
You want to usestrncpy()
instead ofch = sh;
.
– Joey Mallone
Jan 2 at 10:22
Changing the copy of the pointer inStringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.
– Matthieu Brucher
Jan 2 at 10:22
add a comment |
1
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
2
You want to usestrncpy()
instead ofch = sh;
.
– Joey Mallone
Jan 2 at 10:22
Changing the copy of the pointer inStringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.
– Matthieu Brucher
Jan 2 at 10:22
1
1
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
2
2
You want to use
strncpy()
instead of ch = sh;
.– Joey Mallone
Jan 2 at 10:22
You want to use
strncpy()
instead of ch = sh;
.– Joey Mallone
Jan 2 at 10:22
Changing the copy of the pointer in
StringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.– Matthieu Brucher
Jan 2 at 10:22
Changing the copy of the pointer in
StringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.– Matthieu Brucher
Jan 2 at 10:22
add a comment |
2 Answers
2
active
oldest
votes
You want to use strncpy()
instead of ch = sh;
. As other people have kindly pointed, you are not modifying ch
. You are modifying the local variable sh
only.
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
strncpy(ch, sh, 100);
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
OP should usestring
instead.
– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is taggedc++
but then again he is usingchar arrays
and including thecstring
header file. It is a bit of both worlds. :P
– Joey Mallone
Jan 2 at 10:32
Why dostrncpy()
work but notch = sh
?
– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
@sparrowch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), soch
inStringReverse
is actually a copy of the pointerch
in themain
function. When you doch = sh
, you modifych
, the copy, and not the originalch
.strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.
– Holt
Jan 2 at 11:03
|
show 2 more comments
The ch
in StringReverse
function is local to that function. And that's why when assign ch = sh;
, it doesn't change the ch
in main
.
While you can copy sh
, a better approach is to use reverse in-place so that you wouldn't need a local copy at all.
For example:
void StringReverse(char *ch, int size){
for(int i = 0, j= size - 1 ; i < j ; i++, j--){
int t = ch[i];
ch[i] = ch[j];
ch[j] = t;
}
cout << ch << endl;
}
If you use std::string
instead of a plain char array, it would be a lot easier to do this. You could for example use std::reverse
.
You can usestd::reverse
on a simple array, just usech
andch + size
for the iterator.
– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004535%2fhow-modify-character-arrays-inside-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want to use strncpy()
instead of ch = sh;
. As other people have kindly pointed, you are not modifying ch
. You are modifying the local variable sh
only.
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
strncpy(ch, sh, 100);
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
OP should usestring
instead.
– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is taggedc++
but then again he is usingchar arrays
and including thecstring
header file. It is a bit of both worlds. :P
– Joey Mallone
Jan 2 at 10:32
Why dostrncpy()
work but notch = sh
?
– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
@sparrowch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), soch
inStringReverse
is actually a copy of the pointerch
in themain
function. When you doch = sh
, you modifych
, the copy, and not the originalch
.strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.
– Holt
Jan 2 at 11:03
|
show 2 more comments
You want to use strncpy()
instead of ch = sh;
. As other people have kindly pointed, you are not modifying ch
. You are modifying the local variable sh
only.
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
strncpy(ch, sh, 100);
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
OP should usestring
instead.
– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is taggedc++
but then again he is usingchar arrays
and including thecstring
header file. It is a bit of both worlds. :P
– Joey Mallone
Jan 2 at 10:32
Why dostrncpy()
work but notch = sh
?
– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
@sparrowch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), soch
inStringReverse
is actually a copy of the pointerch
in themain
function. When you doch = sh
, you modifych
, the copy, and not the originalch
.strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.
– Holt
Jan 2 at 11:03
|
show 2 more comments
You want to use strncpy()
instead of ch = sh;
. As other people have kindly pointed, you are not modifying ch
. You are modifying the local variable sh
only.
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
strncpy(ch, sh, 100);
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
You want to use strncpy()
instead of ch = sh;
. As other people have kindly pointed, you are not modifying ch
. You are modifying the local variable sh
only.
#include <iostream>
#include <cstring>
using namespace std;
void StringReverse(char *ch, int size){
char sh[100] = {0};
for(int i=0 ; i<size ; i++){
sh[i] = ch[size-1-i];
}
sh[size] = '';
strncpy(ch, sh, 100);
cout<<ch<<endl;
}
int main(){
char ch[100];
cin.getline(ch, 100);
int size = strlen(ch);
StringReverse(ch,size);
cout<<ch;
}
answered Jan 2 at 10:26
Joey MalloneJoey Mallone
2,26561933
2,26561933
OP should usestring
instead.
– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is taggedc++
but then again he is usingchar arrays
and including thecstring
header file. It is a bit of both worlds. :P
– Joey Mallone
Jan 2 at 10:32
Why dostrncpy()
work but notch = sh
?
– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
@sparrowch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), soch
inStringReverse
is actually a copy of the pointerch
in themain
function. When you doch = sh
, you modifych
, the copy, and not the originalch
.strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.
– Holt
Jan 2 at 11:03
|
show 2 more comments
OP should usestring
instead.
– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is taggedc++
but then again he is usingchar arrays
and including thecstring
header file. It is a bit of both worlds. :P
– Joey Mallone
Jan 2 at 10:32
Why dostrncpy()
work but notch = sh
?
– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
@sparrowch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), soch
inStringReverse
is actually a copy of the pointerch
in themain
function. When you doch = sh
, you modifych
, the copy, and not the originalch
.strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.
– Holt
Jan 2 at 11:03
OP should use
string
instead.– Matthieu Brucher
Jan 2 at 10:30
OP should use
string
instead.– Matthieu Brucher
Jan 2 at 10:30
@MatthieuBrucher, Indeed. The question is tagged
c++
but then again he is using char arrays
and including the cstring
header file. It is a bit of both worlds. :P– Joey Mallone
Jan 2 at 10:32
@MatthieuBrucher, Indeed. The question is tagged
c++
but then again he is using char arrays
and including the cstring
header file. It is a bit of both worlds. :P– Joey Mallone
Jan 2 at 10:32
Why do
strncpy()
work but not ch = sh
?– sparrow
Jan 2 at 11:00
Why do
strncpy()
work but not ch = sh
?– sparrow
Jan 2 at 11:00
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
Yes. Using the strings would make it a lot easier I know. I am just clearing out my concepts :P @MatthieuBrucher
– sparrow
Jan 2 at 11:01
1
1
@sparrow
ch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), so ch
in StringReverse
is actually a copy of the pointer ch
in the main
function. When you do ch = sh
, you modify ch
, the copy, and not the original ch
. strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.– Holt
Jan 2 at 11:03
@sparrow
ch
is actually passed by value, i.e., you are passing a pointer by value (because array decay to pointers), so ch
in StringReverse
is actually a copy of the pointer ch
in the main
function. When you do ch = sh
, you modify ch
, the copy, and not the original ch
. strncpy
works on the "pointed" array, which is why it works because both the original and the copy points to the same array.– Holt
Jan 2 at 11:03
|
show 2 more comments
The ch
in StringReverse
function is local to that function. And that's why when assign ch = sh;
, it doesn't change the ch
in main
.
While you can copy sh
, a better approach is to use reverse in-place so that you wouldn't need a local copy at all.
For example:
void StringReverse(char *ch, int size){
for(int i = 0, j= size - 1 ; i < j ; i++, j--){
int t = ch[i];
ch[i] = ch[j];
ch[j] = t;
}
cout << ch << endl;
}
If you use std::string
instead of a plain char array, it would be a lot easier to do this. You could for example use std::reverse
.
You can usestd::reverse
on a simple array, just usech
andch + size
for the iterator.
– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
add a comment |
The ch
in StringReverse
function is local to that function. And that's why when assign ch = sh;
, it doesn't change the ch
in main
.
While you can copy sh
, a better approach is to use reverse in-place so that you wouldn't need a local copy at all.
For example:
void StringReverse(char *ch, int size){
for(int i = 0, j= size - 1 ; i < j ; i++, j--){
int t = ch[i];
ch[i] = ch[j];
ch[j] = t;
}
cout << ch << endl;
}
If you use std::string
instead of a plain char array, it would be a lot easier to do this. You could for example use std::reverse
.
You can usestd::reverse
on a simple array, just usech
andch + size
for the iterator.
– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
add a comment |
The ch
in StringReverse
function is local to that function. And that's why when assign ch = sh;
, it doesn't change the ch
in main
.
While you can copy sh
, a better approach is to use reverse in-place so that you wouldn't need a local copy at all.
For example:
void StringReverse(char *ch, int size){
for(int i = 0, j= size - 1 ; i < j ; i++, j--){
int t = ch[i];
ch[i] = ch[j];
ch[j] = t;
}
cout << ch << endl;
}
If you use std::string
instead of a plain char array, it would be a lot easier to do this. You could for example use std::reverse
.
The ch
in StringReverse
function is local to that function. And that's why when assign ch = sh;
, it doesn't change the ch
in main
.
While you can copy sh
, a better approach is to use reverse in-place so that you wouldn't need a local copy at all.
For example:
void StringReverse(char *ch, int size){
for(int i = 0, j= size - 1 ; i < j ; i++, j--){
int t = ch[i];
ch[i] = ch[j];
ch[j] = t;
}
cout << ch << endl;
}
If you use std::string
instead of a plain char array, it would be a lot easier to do this. You could for example use std::reverse
.
answered Jan 2 at 10:34
usrusr
14.4k32143
14.4k32143
You can usestd::reverse
on a simple array, just usech
andch + size
for the iterator.
– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
add a comment |
You can usestd::reverse
on a simple array, just usech
andch + size
for the iterator.
– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
You can use
std::reverse
on a simple array, just use ch
and ch + size
for the iterator.– Holt
Jan 2 at 11:01
You can use
std::reverse
on a simple array, just use ch
and ch + size
for the iterator.– Holt
Jan 2 at 11:01
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
That's a way better solution. Thanks :)
– sparrow
Jan 2 at 11:03
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
@sparrow If it's an exercise then you're probably expected to implement it rather than use C++ standard library to do everything (so plain array is OK). But you could still go for a better algorithm.
– usr
Jan 2 at 11:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004535%2fhow-modify-character-arrays-inside-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"I believe that the character array is ...". Believing is a dangerous thing. I would test anything first and be sure about it, then just go on belief.
– Joey Mallone
Jan 2 at 10:20
2
You want to use
strncpy()
instead ofch = sh;
.– Joey Mallone
Jan 2 at 10:22
Changing the copy of the pointer in
StringReverse
doesn't change the pointer to a stack variable in another function. Even if it worked, you still want to use a local stack variable, which is a big "no"! Just use a string.– Matthieu Brucher
Jan 2 at 10:22