- 论坛徽章:
- 0
|
原帖由 sunceenjoy 于 2008-9-11 17:18 发表 ![]()
typedef struct {
int count;
char str[1];
} data,*pdata;
pdata pd= (pdata)malloc(sizeof(data)+10);
pd->count=10;
strcpy(pd->str,"10chars");
pd->str[10]='\0';
pdata pd1= (pdata)malloc( ...
我的意思是这样的:
这是一个结构体和指向结构体的指针:
typedef struct {
int count;
char str[0];
}data,*pdata; |
这是一个结构体数组和指向单个结构体数组的指针:
#define N 100
typedef struct {
int count;
char str[0];
}data[N],*pdata; |
如果不知道N的值,或者N的值在程序中有变化,使用上面的方法就比较浪费空间了。
我想通过:
N=get_fun();/** get_fun 返回一个int 值**/
pdata tmp=(pdata)malloc(sizeof(data)*N);/** 分配了变长的结构体空间**/
/** 这个结构体理论上是这样的
int count
char str[0]~~~~~~~~~
**/ |
但是我想通过 tmp 这样的方式来访问 |
|