简体   繁体   English

在C ++中传递数组引用

[英]Passing an Array Reference in C++

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. 我是C ++的新手并且一直遇到很多sytax ..我现在还没有找到具体的答案。 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. 我想将我的变量A传递给函数test并使该函数用局部变量B的内容替换A的内容,然后将其打印出来。 How should this be done? 该怎么做?

reference to array should be this 对数组的引用应该是这样的

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

But in your test function testChar = B; 但是在你的测试函数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 你需要显式字符串复制 (第二个引用不会在C ++中更改,而不是像指针一样),为此您可能希望阅读: C ++ Reference,更改引用的变量

Edit : As @ChristianSjöstedt commented. 编辑 :正如@ChristianSjöstedt所评论的那样。

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. Python是“动态类型语言”,其中变量的类型和值可以改变,而在C ++中,你声明它不会改变的变量的类型。

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

this is possible in Python but not in C++ (and C) 这在Python中是可能的,但在C ++(和C)中则不行

C/C++ are static language mean "statically typed language" . C / C ++是静态语言,意思是“静态类型语言”。 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. i在int中可以是char。

Dynamic type languages versus static type languages 动态类型语言与静态类型语言

"Passing an Array Reference in C++" “在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. 假设您要将对数组的引用传递给函数,并将该数组的元素设置为存储在另一个数组中的元素,则可以使用std::copy ,因为数组不可分配。 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: 但我建议你使用std::array ,它是可复制的和可赋值的:

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

对于固定大小的数组使用std::array ,对于动态(可变长度)数组使用std::vector

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++. 如果你想进行字符串操作,那么你应该从std::string类开始,而不是陷入C ++中数组和指针的可怕混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM