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''数组形式,你可以/> 可以从容器数据中重新创建它。 -Mikeideas?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 aswell: the size of the array, so you can protectagainst an overrun.An alternative to the above is to wrap the array in aclass or struct, and return that. Or store the arraycontents in a standard container (e.g. vector) and returnthat, and if the caller needs the ''raw'' array form, youcan recreate it from the container data.-Mike 这篇关于如何返回一个char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 16:11