简体   繁体   English

段错误内存已转储

[英]segmentation fault memory dumped

I have following code.我有以下代码。 I got a error that 'segmentation fault memory dumped' when I write an array 'ADDRESS.Person' to any value.当我将数组“ADDRESS.Person”写入任何值时,出现“分段错误内存已转储”的错误。 any one please help me to solve a problem.任何人请帮我解决一个问题。

#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;
}

You have just declared the structs , you need to create them as well and hence,SOCIETY is pointing to nothing.Also you are iterating through 32 values whereas there are only 15 in the char array.I have modifed the code ,hopefully you will get an idea here您刚刚声明了结构,您还需要创建它们,因此,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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM