C++ 将字符数组传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8032225/
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
C++ passing char array to function
提问by rpshwin
I would rather just use a string, but we aren't supposed to as the teacher hates them and wants us to figure out ways to avoid them. So I looked into using a struct, but we aren't that far in the book and she hates it when I skip ahead. So I was thinking of doing this:
我宁愿只使用字符串,但我们不应该这样做,因为老师讨厌它们并希望我们想办法避免它们。所以我研究了使用结构,但我们在书中并没有那么远,当我跳过时她讨厌它。所以我想这样做:
#include <iomanip>
#include <iostream>
#include <stdio.h>
using namespace std;
void myfunc(char& );
int main()
{
char myname[12];
cout<<"enter a name ";
cin>>myname;
cout<<"myname is "<<myname;
cout<<"myname is " << myfunc(myname);
getchar();
getchar();
return 0;
}
void myfunc(char &myname1)
{
myname1 = "Billy"
}
But this doesn't work and I don't know why.
但这不起作用,我不知道为什么。
回答by Mysticial
One way is to do it like this:
一种方法是这样做:
void myfunc(char *myname1)
{
strcpy(myname1,"Billy");
}
You will also need to change your main to:
您还需要将 main 更改为:
myfunc(myname);
cout<<"myname is " << myname;
However you have to be careful not to overflow your initial buffer.
但是,您必须小心不要溢出初始缓冲区。
The reason why your original code doesn't work is because you can't assign strings to char
pointers. Instead you must copy the string to the char*
.
您的原始代码不起作用的原因是您无法将字符串分配给char
指针。相反,您必须将字符串复制到char*
.
回答by Juan Manuel
This line of code is wrong:
这行代码是错误的:
cout<<"myname is " << myfunc(myname);
myfunc()
doesn't return anything, its return type is void
.
myfunc()
不返回任何东西,它的返回类型是void
.
Try using:
尝试使用:
char* myfunc(char *myname1)
{
strcpy(myname1,"Billy");
return myname;
}
Or
或者
myfunc(myname);
cout<<"myname is " << myname;
回答by Martin York
Arrays devolve into pointers when passed as parameters.
So the simple way that you want is:
当作为参数传递时,数组会转变为指针。
所以你想要的简单方法是:
char* myfunc(char* myname1)
{
return myname1;
}
If you were going to show off you can pass the array by reference.
But if you can't read ahead you will not be able to use this.
如果您要炫耀,您可以通过引用传递数组。
但如果你不能提前阅读,你将无法使用它。
char* myfunc(char (&myname1)[12]) // Note you can only pass arrays of size 12
{ // to this function so it is limited in use.
return myname1;
}
TO make it more useful though you could template it:
尽管您可以对其进行模板化,但要使其更有用:
template<int SIZE>
char* myfunc(char (&myname1)[SIZE])
{
return myname1;
}
回答by Martin Beckett
myname1 = "Billy"
doesn't copy a string it copies a pointer to the constant local memory containing "Billy"
myname1 = "Billy"
不复制字符串,而是复制指向包含“Billy”的常量本地内存的指针
Take a look at strncpy() or memcpy()
看看 strncpy() 或 memcpy()
回答by John Humphreys - w00te
Pass it as a char* instead of a char&. You're passing a reference to a single character instead of a pointer to a character array in this code.
将其作为 char* 而不是 char& 传递。在此代码中,您传递的是对单个字符的引用,而不是指向字符数组的指针。
Also use strncpy (google it) to set the value of tr char* once you're in the function.
一旦你在函数中,也可以使用 strncpy (google it) 来设置 tr char* 的值。
回答by Alex Force
void myfunc(char& ); is the problem it should take in a char * and not a char reference which is what you did.
void myfunc(char&); 是它应该采用 char * 而不是 char 引用的问题,这就是你所做的。
and in the function use strcpy(char * destination, char *source);
并在函数中使用 strcpy(char * destination, char *source);