C中的双返回

jar*_*ryd 1 c

是否可以在C中返回两个变量?

我想返回一个指针和一个字符串,以便我可以在函数外部使用它们.

这是用数组完成的吗?如果是这样,它会是什么类型的阵列?空虚吗?

Jam*_*lis 6

不,您只能返回一个对象.但是,您可以返回结构类型对象,并且可以将"多个返回"放入结构中:

typedef struct return_type
{
    void* pointer_; // You should, of course, use whatever the appropriate types
    char* string_;  // are for the objects that you are trying to return.
};

return_type f();
Run Code Online (Sandbox Code Playgroud)

  • @Scott:不是,不是.我的意思是,你不想实际命名结构`return_type`; 如果你有一个名为`do_x`的函数,那么你可以命名返回类型结构`do_x_result`或类似的东西. (2认同)
  • @alJaree:为什么要返回`return_type*`?只需返回一个`return_type`对象; 你可以像任何其他结构类型对象一样对待它. (2认同)