为什么在C里可行,而C++不行
#include <stdio.h>
#include <malloc.h>
#define ST_CHAR char
typedef struct sxe_attr_pair
{
ST_CHAR *name;
ST_CHAR *value;
} SXE_ATTR_PAIR;
typedef struct
{
ST_CHAR *Tag;
SXE_ATTR_PAIR *Memory;
} SYS_ENC_INFO;
int main()
{
SYS_ENC_INFO enc ;
scanf ("%s", enc.Tag);
printf ("%s\n", enc.Tag);
return 0;
}
[解决办法]
你只是定义了指针,没有为指针分配地址空间,当然不行!
#include <stdio.h>
#include <malloc.h>
#define ST_CHAR char
typedef struct sxe_attr_pair
{
ST_CHAR *name;
ST_CHAR *value;
} SXE_ATTR_PAIR;
typedef struct
{
ST_CHAR *Tag;
SXE_ATTR_PAIR *Memory;
} SYS_ENC_INFO;
int main()
{
SYS_ENC_INFO enc ;
enc.Tag = new ST_CHAR[10]; // 要使用其他成员也需要在使用前分配地址空间
scanf ("%s", enc.Tag);
printf ("%s\n", enc.Tag);
return 0;
}
[解决办法]
你说的C是用TC吧 20年前的老古董了