我有以下代码。当我将数组“ ADDRESS.Person”写入任何值时,出现一个“段故障内存已转储”的错误。任何人都可以帮助我解决问题。
#include <stdio.h>
typedef struct
{
char Person[15];
} stName;
typedef struct
{
stName Name;
} stSociety;
stSociety* SOCIETY;
#define ADDRESS SOCIETY->Name
int main()
{
int i;
for (i=0; i<32; i++)
{
ADDRESS.Person[i] = 0;
}
printf("ADDRESS.Person=%s\n", ADDRESS.Person);
printf("Finished");
return 0;
}
答案 0 :(得分:-2)
您刚刚声明了这些结构体,因此还需要创建它们,因此,SOCIETY指向任何内容。此外,您正在遍历32个值,而char数组中只有15个。我修改了代码,希望您会在这里得到一个想法
#include <stdio.h>
typedef struct
{
char Person[15];
} stName;
typedef struct
{
stName Name;
} stSociety;
#define ADDRESS SOCIETY->Name
int main()
{
stSociety* SOCIETY,sample;
SOCIETY = &sample;
int i;
for (i=0; i<15; i++)
{
ADDRESS.Person[i] = '0';
}
printf("ADDRESS.Person=%s\n", ADDRESS.Person);
printf("Finished");
return 0;
}