C语言结构的灵活(变长)数组成员为何不能用指针代替
/* 使用b1代替a1,b2代替a2,则出错。 */
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct S
{
int len;
char result[1];
//char result[]; //如果是C99编译器,result可以这样声明,代替上一句。
} S;
typedef struct S2
{
int len;
char * result;
} S2;
int main()
{
char * pszHello = "hello, world";
S * pS; //a1
//S2 * pS; //b1
pS = (S *)(malloc(sizeof(S) + strlen(pszHello) + 1)); //a2
//pS = (S2 *)(malloc(sizeof(S2) + strlen(pszHello) + 1)); //b2
pS->len = 122;
strcpy(pS->result, pszHello);
printf("%s\n", pS->result);
free(pS);
}