如何返回一个char数组 [英] how to return a char array
问题描述
我一直试图在
函数结束时返回一个数据包(作为一个char数组)但是这似乎是是不可能的......
我想在不同的类中使用这个char数组。有什么想法吗?
greetz,
Ger
Hi,
I have been trying to return a data packet (as a char array) at the end of a
function but that seems to be impossible...
I would like to use this char array in a different class..anyone some ideas?
greetz,
Ger
推荐答案
" Ger" <克****** @ geenspam.student.utwente.nl>在消息中写道
news:bv ********** @ ares.cs.utwente.nl ...
"Ger" <g.******@geenspam.student.utwente.nl> wrote in message
news:bv**********@ares.cs.utwente.nl...
<我一直试图在
a函数的末尾返回一个数据包(作为一个char数组),但这似乎是不可能的...
我想在一个char数组中使用它不同的班级。一些
想法吗?
greetz,
Ger
Hi,
I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible...
I would like to use this char array in a different class..anyone some ideas?
greetz,
Ger
您可以:
1返回一个指向数组的char *指针(最好不要是自动数组)。
2创建一个包含数组的结构并返回其中一个(几乎是
肯定是一个坏主意)
3.传递对std :: vector的引用并填充它(可能是最好的选择
而不知道更多)
You can either:
1 return a char* pointer to an array (better not be an automatic one).
2 create a struct with an array in it and return one of those (almost
certainly a bad idea)
3. pass in a reference to a std::vector and fill it (pprobably the best bet
without knowing more)
Ger写道:
Ger writes:
我一直试图在
a函数结束时返回一个数据包(作为一个char数组)但这似乎是不可能的......
我想在另一个类中使用这个char数组一些
I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible...
I would like to use this char array in a different class..anyone some
想法?
在*
$ b $之前使用new,在某些函数*中为char数组分配空间b调用''数据包''功能。将指针传递给
,直到其有意义的生命周期到期为止。然后用删除删除它。
分配和删除不必在同一个函数中执行。
ideas?
Allocate space for the char array, using new, in some function *before*
calling the ''data packet'' function. Pass a pointer to this array around
until its meaningful lifetime has expired. Then delete it with delete. The
assignment and deletion do not have to be performed in the same function.
" Ger" <克****** @ geenspam.student.utwente.nl>在消息中写道
news:bv ********** @ ares.cs.utwente.nl ...
"Ger" <g.******@geenspam.student.utwente.nl> wrote in message
news:bv**********@ares.cs.utwente.nl...
<我一直试图在
a函数结束时返回一个数据包(作为一个char数组),但这似乎是不可能的......
对。向/从函数传递和返回数组是不允许的。
。改为通过指针。
我想在另一个类中使用这个char数组。一些
Hi,
I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible...
Right. Passing and returning arrays to/from functions is
not allowed. Pass a pointer instead.
I would like to use this char array in a different class..anyone some
想法?
>
void get_packet(char * arg)
{
strcpy(arg," data");
some_function_that_gets_packet(arg );
}
int main()
{
char * array = new char [DESIRED_SIZE];
get_packet(array);
cout<<数组<< \\\
; //输出数据
}
发送第二个参数也是个好主意
好吧:数组的大小,所以你可以保护
免受溢出。
上面的替代方法是将数组包装在一个
类或结构,并返回。或者将数组
内容存储在一个标准容器(例如vector)中并返回
,如果调用者需要''raw''数组形式,你可以/>
可以从容器数据中重新创建它。
-Mike
ideas?
void get_packet(char *arg)
{
strcpy(arg, "data");
some_function_that_gets_packet(arg);
}
int main()
{
char *array = new char[DESIRED_SIZE];
get_packet(array);
cout << array << ''\n''; // outputs "data"
}
It''s also a good idea to send a second argument as
well: the size of the array, so you can protect
against an overrun.
An alternative to the above is to wrap the array in a
class or struct, and return that. Or store the array
contents in a standard container (e.g. vector) and return
that, and if the caller needs the ''raw'' array form, you
can recreate it from the container data.
-Mike
这篇关于如何返回一个char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!