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?

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • 1
    An array is not a `char&`, and you cannot assign arrays to one another. Which C++ book are you using? – Lightness Races in Orbit Apr 10 '13 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 – Sander De Dycker Apr 10 '13 at 10:13
  • @SanderDeDycker: In what sense does that action differ from _assignment_? – Lightness Races in Orbit Apr 10 '13 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.). – Sander De Dycker Apr 10 '13 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. – Lightness Races in Orbit Apr 10 '13 at 10:58

4 Answers4

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

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    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 – Christian Sjöstedt Apr 10 '13 at 10:36
  • @ChristianSjöstedt welcom! :) you should consider juanchopanza's answer. he posted good technique. – Grijesh Chauhan Apr 10 '13 at 10:37
  • @ChristianSjöstedt wait Python is dynamic language where as c++ and c are not, wait I updated something. – Grijesh Chauhan Apr 10 '13 at 10:39
  • @ChristianSjöstedt By the change I am not good in both but I hope upto some extend this will help you. Thanks! – Grijesh Chauhan Apr 10 '13 at 10:45
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;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

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

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
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++.

john
  • 7,897
  • 29
  • 27