C++ 将字符数组传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10383615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:59:50  来源:igfitidea点击:

Passing char array into a function

c++arraysfunctionchar

提问by Daniel Del Core

How do you pass a char array into a function.

如何将字符数组传递给函数。

declarations

声明

char fromName[64];
char fromStreet[64];
char fromSuburb[64];
char fromCountry[64];

function call

函数调用

    Trans[i]->putAddress(fromName, fromStreet, fromSuburb, fromCountry);

prototype

原型

void putAddress(char,char,char,char);

function    
void putAddress(char fName,char fStreet,char fSuburb,char fCountry){

        return;
}

and Error "main.cpp", line 86: Error: Formal argument 1 of type char in call to Mail::putAddress(char, char, char, char) is being passed char*.

和错误“main.cpp”,第 86 行:错误:在调用 Mail::putAddress(char, char, char, char) 时,char 类型的正式参数 1 被传递为 char*。

采纳答案by Cratylus

Your function should be:

你的功能应该是:

void putAddress(char *,char *,char *,char *);

回答by EdChum

You need to pass pointers to char

您需要将指针传递给 char

void putAddress(char* fName,char* fStreet,char* fSuburb,char* fCountry);

You then need to be careful you know the size of each array so you don't index off the end, in your case all of them are 64.

然后你需要小心你知道每个数组的大小,这样你就不会索引到最后,在你的情况下,它们都是 64。

回答by iammilind

You can pass an array in 2 ways:

您可以通过两种方式传递数组:

(1) Conventional C-style:
Here you pass by address and receive using a pointer

(1) 传统的 C 风格:
这里你传递地址并使用指针接收

void putAddress(char *,char *,char *,char *);

(2) C++ pass by reference:
You pass the array by reference with size specification:

(2) C++ 通过引用传递:
您通过引用传递数组,并指定大小:

 void putAddress(char (&a1)[64], char (&a2)[64],char (&a3)[64], char (&a4)[64]);

This helps you getting the array-size straight away correct (pointer is not allowed). This can be made more sophisticated using templatealso.

这有助于您立即获得正确的数组大小(不允许使用指针)。这也可以变得更加复杂template

You can also iterate the option of using std::string, which will make a copy of the whole array and manage it as an automatic variable.

您还可以迭代 using 选项std::string,这将复制整个数组并将其作为自动变量进行管理。

回答by Jonathon Reinhart

The compiler is telling you right there... Its being passed as char*. So use either char*or char ar[].

编译器就在那里告诉你......它被传递为char*. 所以使用char*char ar[]

回答by orlp

You pass strings (arrays of characters) as a pointer to the first character of the array:

您将字符串(字符数组)作为指向数组第一个字符的指针传递:

void something(char *str) { /* ... */ }

int main(int argc, char **argv) {
    char somestring[] = "Hell World!\n";

    something(somestring);

    return 0;
}

Because arrays automatically decay to pointers when passed to a function all you have to do is pass the character array and it works. So in your example:

因为数组在传递给函数时会自动衰减为指针,所以您要做的就是传递字符数组并且它可以工作。所以在你的例子中:

void putAddress(char*, char*, char*, char*);

回答by aaronqli

void putAddress(char* array){
    //use array as usual
}

回答by Luchian Grigore

To correct your code:

要更正您的代码:

void putAddress(char*,char*,char*,char*);

but it's still wrong. Arrays decay to pointers, that's why it will compile, but will result in an error if the arguments are not null-terminated. You should also pass in the size if you choose this approach.

但它仍然是错误的。数组衰减为指针,这就是它会编译的原因,但如果参数不是以 null 结尾的,则会导致错误。如果您选择这种方法,您还应该传递大小。

However, since this is C++ and not C, I suggest you use std::stringinstead:

但是,由于这是 C++ 而不是 C,我建议您std::string改用:

void putAddress(const std::string&,const std::string&,const std::string&,const std::string&);

回答by phantasmagoria

void putAddress(char[],char[],char[],char[]);

function    
void putAddress(char fName[],char fStreet[],char fSuburb[],char fCountry[]){

        return;
}

You have forgotten to put the paranthesis, Put them as in the above code.

你忘了把括号放在上面,把它们放在上面的代码中。

回答by biril

The compiler's error makes sence as fromName is indeed a pointer to the (first element of the) fromName array. This is just C++ (and plain C) syntax.

编译器的错误是有意义的,因为 fromName 确实是指向 fromName 数组(的第一个元素)的指针。这只是 C++(和普通 C)语法。

In order to pass a char array to a function you should do what you are currently doing, that is, pass a pointer to the (first element of the) array.

为了将 char 数组传递给函数,您应该执行当前正在执行的操作,即传递指向(数组的第一个元素)的指针。

So all you need to do is change

所以你需要做的就是改变

    void putAddress(char,char,char,char);

to

    void putAddress(char *, char *, char *, char *);

PS: Your next problem is knowing (making putAddress aware of) each array's length. If these are fixed though, you have no problem.

PS:你的下一个问题是知道(让 putAddress 知道)每个数组的长度。如果这些都是固定的,你就没有问题。