How modify character arrays inside functions?












0















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.










share|improve this question




















  • 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 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
















0















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.










share|improve this question




















  • 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 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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 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








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












2 Answers
2






active

oldest

votes


















0














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;
}





share|improve this answer
























  • 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













  • 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








  • 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





















0














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.






share|improve this answer
























  • 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











  • @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











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
});


}
});














draft saved

draft discarded


















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









0














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;
}





share|improve this answer
























  • 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













  • 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








  • 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


















0














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;
}





share|improve this answer
























  • 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













  • 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








  • 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
















0












0








0







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;
}





share|improve this answer













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;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 10:26









Joey MalloneJoey Mallone

2,26561933




2,26561933













  • 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













  • 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








  • 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





















  • 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













  • 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








  • 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



















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















0














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.






share|improve this answer
























  • 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











  • @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
















0














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.






share|improve this answer
























  • 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











  • @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














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 10:34









usrusr

14.4k32143




14.4k32143













  • 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











  • @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











  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]