方法一: 参数里含有指向指针的指针。
注意:如果函数参数里只有一个指向结构体的指针,是无法正确地返回结构体的值的。原因在于在编译的时候,会对入参p产生一个备份_p.
参考此文:http://www.cnblogs.com/kaituorensheng/p/3246900.html
方法二:返回一个指向结构体的函数指针
#include "stdafx.h"
#include "stdlib.h"
#include "stdint.h"
typedef struct Vector3
{
int X;
int Y;
int Z;
} Vector3;
typedef struct Config
{
int mode;
Vector3 *pData;
} Config;
void GetConfigData(Config** pConfig)
{
Config *cfg = (Config *)malloc(sizeof(Config));
cfg->mode = 1;
cfg->pData = (Vector3 *)malloc(sizeof(Vector3));
cfg->pData->X = 2;
cfg->pData->Y = 4;
cfg->pData->Z = 6;
*pConfig = cfg;
}
Config *GetConfigData1()
{
Config *cfg = (Config *)malloc(sizeof(Config));
cfg->mode = 3;
cfg->pData = (Vector3 *)malloc(sizeof(Vector3));
cfg->pData->X = 5;
cfg->pData->Y = 7;
cfg->pData->Z = 9;
return cfg;
}
int _tmain(int argc, _TCHAR* argv[])
{
Config *cfg1 = (Config *)malloc(sizeof(Config));
cfg1->pData = (Vector3 *)malloc(sizeof(Vector3));
GetConfigData(&cfg1);
printf("cfg1:%d, %d, %d, %d", cfg1->mode, cfg1->pData->X, cfg1->pData->Y, cfg1->pData -> Z);
cfg1 = GetConfigData1();
printf("cfg1:%d, %d, %d, %d", cfg1->mode, cfg1->pData->X, cfg1->pData->Y, cfg1->pData->Z);
free(cfg1);
}