C++ 将指针作为输出参数传递给函数
我使用“pBuff”作为指针,并在函数“myfunc”中放置一个字符数组。 所以,在主要功能中,我应该在aBuff中接收它 但是它不起作用。。这里怎么了C++ 将指针作为输出参数传递给函数,c++,pointers,C++,Pointers,我使用“pBuff”作为指针,并在函数“myfunc”中放置一个字符数组。 所以,在主要功能中,我应该在aBuff中接收它 但是它不起作用。。这里怎么了 #include <stdio.h> void myfunc(void *pBuff, int &i); int main() { int len; char aBuff[2]={0}; printf("Hello World"); myfunc(aBuff,len); printf
#include <stdio.h>
void myfunc(void *pBuff, int &i);
int main()
{
int len;
char aBuff[2]={0};
printf("Hello World");
myfunc(aBuff,len);
printf("aBuff %s", aBuff);
return 0;
}
myfunc(void *pBuff, int &i){
char a[2] = {'a', 'b'};
i = 5;
pBuff = &a;
}
#包括
void myfunc(void*pBuff,int&i);
int main()
{
内伦;
char aBuff[2]={0};
printf(“你好世界”);
myfunc(阿伯夫,len);
printf(“阿布夫%s”,阿布夫);
返回0;
}
myfunc(无效*pBuff,int&i){
字符a[2]={'a','b'};
i=5;
pBuff=&a;
}
char a[]
应作为主函数中的输出参数如果要返回数组,有几种可能的解决方案:
type*
)传递到函数并复制数据,例如memcpy
。您还应该传递目标阵列的大小,并确保不会复制太多数据类型*&
),并将其分配给函数中的指针类型**
),并将地址分配给取消引用的指针a
(它是指向第一个元素的指针),则它必须是静态的
,或者使用例如malloc
动态分配的
选择哪种解决方案取决于您的需要:
3种变体的示例:
#include <stdio.h>
#include <memory.h>
void myfunc(char **pBuff, int &i);
void myfunc2(char *&pBuff, int &i);
void myfunc1(char *pBuff, size_t bufsize, int &i);
int main()
{
int len;
char aBuff[3]={0}; // increased size for NUL termination
char *ptr = aBuff;
printf("Hello World");
myfunc(&ptr,len);
printf("ptr %s", ptr);
printf("\n");
myfunc2(ptr,len);
printf("ptr %s", ptr);
printf("\n");
myfunc1(aBuff, sizeof aBuff,len);
printf("aBuff %s", aBuff);
return 0;
}
void myfunc1(char *pBuff, size_t bufsize, int &i){
char a[3] = {'c', 'd', '\0'}; /* added '\0' to allow using it as a string */
i = 5;
// actually the minimum of bufsize and sizeof(a) should be used
memcpy(pBuff, a, bufsize);
}
void myfunc(char **pBuff, int &i){
static char a[3] = {'a', 'b', '\0'}; // added '\0' to allow using it as a string
i = 5;
*pBuff = a;
}
void myfunc2(char *&pBuff, int &i){
static char a[] = "xyz";
i = 5;
pBuff = a;
}
#包括
#包括
无效myfunc(字符**pBuff,int&i);
void myfunc2(char*&pBuff,int&i);
void myfunc1(char*pBuff,size\u t bufsize,int&i);
int main()
{
内伦;
char aBuff[3]={0};//增加了NUL终止的大小
char*ptr=aBuff;
printf(“你好世界”);
myfunc(&ptr,len);
printf(“ptr%s”,ptr);
printf(“\n”);
myfunc2(ptr,len);
printf(“ptr%s”,ptr);
printf(“\n”);
myfunc1(阿布夫,阿布夫大小,len);
printf(“阿布夫%s”,阿布夫);
返回0;
}
void myfunc1(char*pBuff,size\u t bufsize,int&i){
字符a[3]={'c','d','\0'};/*添加了'\0'以允许将其用作字符串*/
i=5;
//实际上,应该使用bufsize和sizeof(a)的最小值
memcpy(pBuff,a,bufsize);
}
void myfunc(字符**pBuff,int&i){
静态字符a[3]={'a','b','\0'};//添加了'\0'以允许将其用作字符串
i=5;
*pBuff=a;
}
void myfunc2(char*&pBuff,int&i){
静态字符a[]=“xyz”;
i=5;
pBuff=a;
}
您正在传递一个指向函数的指针,在函数内部您将临时变量a
的地址分配给pBuff
变量。原始aBuff
变量不受影响
我们使用C++时,使用字符串的更简单的解决方案是:
#include <iostream>
void myfunc(std::string& pBuff, int &i);
int main()
{
int len;
std::string aBuff;
std::cout << "Hello World";
myfunc(aBuff,len);
std::cout << "aBuff " << aBuff;
return 0;
}
void myfunc(std::string& pBuff, int &i){
pBuff = "ab";
i = 5;
}
如果需要在
aBuff
数组中存储2个字符,则需要3个字符长而不是2个字符,因为需要为空终止符留出空间,否则printf
和其他字符串函数将无法正常工作。I don;我不认为您可以,毕竟您正在尝试从函数返回一个本地数组。但是,我正在传递a
的地址。或者,我如何才能将char a[]
取回?这里出了什么问题???
您将引用(&a)带到一个临时文件,该文件在myfunc返回后被销毁。。。为什么PBUFF是空指针?为什么这个问题C++被标记而不是C?它是用双指针工作的。代码>无效myfunc(无效**pBuff,int&i)代码>-1:1)aBuff
的大小为2,但是myfunc/myfunc2放入了3个字符->堆栈溢出。2) 不使用static只是一个糟糕的建议。3) 这里不需要指向指针的指针或指向指针的引用,它实际上会导致在调用myFunc或myFunc2后,根本不使用ABFF。@user1810087myFunc
和myFunc2
不覆盖ABFF
,而是为ptr分配一个新地址。返回静态
缓冲区是否坏取决于此处未明确的要求。当然,函数不能通过指定指针来修改aBuff
。myfunc和myfunc2不会覆盖aBuff,而是将新地址指定给ptr
——正是这样,使aBuff作为缓冲区变得毫无意义<代码>静态缓冲区是否损坏取决于要求-正确,但在大多数情况下它是不必要的。总的来说,我(我想其他一些人也一样)会说,静电应该很少使用,而且要小心。当你调用MyFunc时,这个函数的用户提供了一个不使用的缓冲区!而是为myfunc的所有调用方使用静态(全局/共享)缓冲区!那太糟糕了。更糟糕的是,myfunc1/myfunc2使用不同的静态缓冲区。编辑后:myfunc1似乎进入了正确的方向。但静态仍然是错误的。一些清理和适当的边界检查仍然很好:)
myfunc(char *pBuff, int &i){
strcpy(pBuff, "ab");
i = 5;
}