0

I'm new to C++ and running into a lot of sytax all the time.. I havn't been able to find a concrete answer to this in a while now. I'm trying to run this piece of code:

void test(char &testChar){ char B[3] = {"BB"}; testChar = B; } int main(int argc, const char * argv[]) { char A[3] = {"AB"}; test(A); std::cout << A; return 0; } 

I want to pass my Variable A to function test and have that function replace the content of A with the content of local variable B, and then print that out. How should this be done?

5
  • 1
    An array is not a char&, and you cannot assign arrays to one another. Which C++ book are you using? Commented Apr 10, 2013 at 10:12
  • 1
    @LightnessRacesinOrbit: he's not asking to assign an array to another - he's asking to replace the content of an array with that of another Commented Apr 10, 2013 at 10:13
  • @SanderDeDycker: In what sense does that action differ from assignment? Commented Apr 10, 2013 at 10:30
  • @LightnessRacesinOrbit: you cannot assign an array to another (using an assignment operator) - you can copy the contents of an array to another (using a loop eg.). Commented Apr 10, 2013 at 10:33
  • @SanderDeDycker: Yet, beyond the fact that C++ allows one and not the other (for legacy reasons), semantically the two operations are utterly indistinct. What do you think happens when you do int x = 5, y = 4; y = x;? You assign x to y, which replaces the contents of the int with that of another. What Christian cannot do is use assignment notation -- he is absolutely attempting to do that at the present time, as you can clearly see from his code, and it is this that I refer to. Commented Apr 10, 2013 at 10:58

4 Answers 4

1

reference to array should be this

void test(char (&testChar)[3]){ // code } 

But in your test function testChar = B; expression is wrong. you need to explicitly string copy (second reference doesn't change in C++, not like pointer) for this you may like to read: C++ Reference, change the refered variable

Edit: As @ChristianSjöstedt commented.

Python is "dynamic typed language" where type and value of variable can be change, Where as in C++ one you declare the type of a variable it doesn't change.

 i = 10 # reference to int i = "name" # reference to str 

this is possible in Python but not in C++ (and C)

C/C++ are static language mean "statically typed language" . for example type of a variable can't be change and defined statically at compilation time.

int i = 10; 

i in int can be char.

Dynamic type languages versus static type languages

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick responses! I'm coming form a python background where everything just works so its a lot of re-schooling going on for me right now. doing this: (char (&testChar)[3]) and assigning elements individually solved my problem. thanks again
@ChristianSjöstedt welcom! :) you should consider juanchopanza's answer. he posted good technique.
@ChristianSjöstedt wait Python is dynamic language where as c++ and c are not, wait I updated something.
@ChristianSjöstedt By the change I am not good in both but I hope upto some extend this will help you. Thanks!
1

"Passing an Array Reference in C++"

Assuming you want to pass a reference to an array to a function, and set the elements of that array to those stored in another one, you can use std::copy, since arrays are not assignable. It is better to use a template function to have a handle on the size of the array:

template <size_t N> test( char (&testChar)[N] ) { char B[N] = {"BB"}; stc::copy(B, B+N, testChar); } 

But I suggest you use std::array, which is copyable and assignable:

template <size_t N> test(std::array<char,N>& testarray; ) { std::array<char, N> B = {"BB"}; teasArray = B; } 

2 Comments

And you still won't be able to assign one to the other.
@ChristianSjöstedt: Your C++ book covers this, too. It would save us from answering this again (it has already been done many times) if you'd read it!
0

Use either std::array for fixed size arrays or std::vector for dynamic (variable length) ones.

Comments

0

This is the code you are probably looking for

#include <string.h> void test(char *testChar){ char B[3] = {"BB"}; strcpy(testChar, B); } int main(int argc, const char * argv[]) { char A[3] = {"AB"}; test(A); std::cout << A; return 0; } 

but there are all sorts of reasons why code like this is a bad idea. If you want to do string manipulation then you should start with the std::string class instead of getting into the horrible mess that is arrays and pointers in C++.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.