如何修复strcat函数中的分段错误?

如何修复strcat函数中的分段错误?,c,struct,function-pointers,C,Struct,Function Pointers,我试图将函数指针用作C结构的成员。我有Identity、Person和RandomPeople类型。我的程序以“program has stop working”结尾。我用gdb调试了我的程序,我有以下输出 [New Thread 18028.0x2c28] [New Thread 18028.0x4150] enter the number of people:2 Program received signal SIGSEGV, Segmentation fault. 0x754c5619 i

我试图将函数指针用作C结构的成员。我有Identity、Person和RandomPeople类型。我的程序以“program has stop working”结尾。我用gdb调试了我的程序,我有以下输出

[New Thread 18028.0x2c28]
[New Thread 18028.0x4150]
enter the number of people:2
Program received signal SIGSEGV, Segmentation fault.
0x754c5619 in strcat () from C:\WINDOWS\SysWOW64\msvcrt.dll
(可能
strcat
inRandomPeople.h

这是我的节目:
标识结构
可以创建标识号并检查给定的标识号

Identity.h文件:

#ifndef Identity_H
#define Identity_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

struct IDNO {
    char *(*CreateIDNo)(struct IDNO *);   
};

typedef struct IDNO *Id;
Id CreateID();
char *CreateIDNo(Id this);
#endif
#include "Identity.h"

char *CreateIDNo(Id this) { 
    int str[11]; 
    int totalodd = 0;
    int totaleven = 0;
    this->id = "";

    for (int i = 1; i < 12; i++) {           
        if (i == 1) {
            int n = 1 + rand() % 9;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 == 0 && i < 10) {
            int n = rand() % 10;
            totaleven += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 != 0 && i < 10) {
            int n = rand() %10;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i == 10) {
            int n11 = (7 * totalodd - totaleven) % 10;
            str[i - 1] = n11;
            continue;
        } else
        if (i == 11) {
            int n12 = (totalodd + totaleven + str[9]) % 10;
            str[i - 1] = n12;
            continue;
        }      
    }
    for (int i = 0; i < 11; i++) {
        char *b; 
        itoa(str[i], b, 10);
        strcat(this->id, b);
    }
    return this->id;
}
#ifndef PERSON_H
#define PERSON_H
#include "Identity.h"

struct PERSON {
    Id superid;
};

typedef struct PERSON *Person;
Person CreatePerson();
#endif
#include "Person.h"

Person CreatePerson() {
    Person this;
    this = (Person)malloc(sizeof(struct PERSON));
    this->superid = CreateID();  //Creating my reference    
    return this;
}
#include "RandomPeople.h"

void CreateRandomPeopleData(RandomPeople k) {
    Person person = CreatePerson();
    char *formatted_identity = person->superid->CreateIDNo(person->superid);
    strcat(data, formatted_identity);       
}   
Person.c文件:

#ifndef Identity_H
#define Identity_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

struct IDNO {
    char *(*CreateIDNo)(struct IDNO *);   
};

typedef struct IDNO *Id;
Id CreateID();
char *CreateIDNo(Id this);
#endif
#include "Identity.h"

char *CreateIDNo(Id this) { 
    int str[11]; 
    int totalodd = 0;
    int totaleven = 0;
    this->id = "";

    for (int i = 1; i < 12; i++) {           
        if (i == 1) {
            int n = 1 + rand() % 9;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 == 0 && i < 10) {
            int n = rand() % 10;
            totaleven += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 != 0 && i < 10) {
            int n = rand() %10;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i == 10) {
            int n11 = (7 * totalodd - totaleven) % 10;
            str[i - 1] = n11;
            continue;
        } else
        if (i == 11) {
            int n12 = (totalodd + totaleven + str[9]) % 10;
            str[i - 1] = n12;
            continue;
        }      
    }
    for (int i = 0; i < 11; i++) {
        char *b; 
        itoa(str[i], b, 10);
        strcat(this->id, b);
    }
    return this->id;
}
#ifndef PERSON_H
#define PERSON_H
#include "Identity.h"

struct PERSON {
    Id superid;
};

typedef struct PERSON *Person;
Person CreatePerson();
#endif
#include "Person.h"

Person CreatePerson() {
    Person this;
    this = (Person)malloc(sizeof(struct PERSON));
    this->superid = CreateID();  //Creating my reference    
    return this;
}
#include "RandomPeople.h"

void CreateRandomPeopleData(RandomPeople k) {
    Person person = CreatePerson();
    char *formatted_identity = person->superid->CreateIDNo(person->superid);
    strcat(data, formatted_identity);       
}   
RandomPeople.c文件:

#ifndef Identity_H
#define Identity_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

struct IDNO {
    char *(*CreateIDNo)(struct IDNO *);   
};

typedef struct IDNO *Id;
Id CreateID();
char *CreateIDNo(Id this);
#endif
#include "Identity.h"

char *CreateIDNo(Id this) { 
    int str[11]; 
    int totalodd = 0;
    int totaleven = 0;
    this->id = "";

    for (int i = 1; i < 12; i++) {           
        if (i == 1) {
            int n = 1 + rand() % 9;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 == 0 && i < 10) {
            int n = rand() % 10;
            totaleven += n;
            str[i - 1] = n;
            continue;
        } else
        if (i != 1 && i % 2 != 0 && i < 10) {
            int n = rand() %10;
            totalodd += n;
            str[i - 1] = n;
            continue;
        } else
        if (i == 10) {
            int n11 = (7 * totalodd - totaleven) % 10;
            str[i - 1] = n11;
            continue;
        } else
        if (i == 11) {
            int n12 = (totalodd + totaleven + str[9]) % 10;
            str[i - 1] = n12;
            continue;
        }      
    }
    for (int i = 0; i < 11; i++) {
        char *b; 
        itoa(str[i], b, 10);
        strcat(this->id, b);
    }
    return this->id;
}
#ifndef PERSON_H
#define PERSON_H
#include "Identity.h"

struct PERSON {
    Id superid;
};

typedef struct PERSON *Person;
Person CreatePerson();
#endif
#include "Person.h"

Person CreatePerson() {
    Person this;
    this = (Person)malloc(sizeof(struct PERSON));
    this->superid = CreateID();  //Creating my reference    
    return this;
}
#include "RandomPeople.h"

void CreateRandomPeopleData(RandomPeople k) {
    Person person = CreatePerson();
    char *formatted_identity = person->superid->CreateIDNo(person->superid);
    strcat(data, formatted_identity);       
}   
test.c文件

int main() {
    RandomPeople rastgelekisiler = CreateRandomPeople();
    rastgelekisiler->CreateRandomPeopleData(rastgelekisiler);
    return 0;
}

编辑:我把这些结构放在我的问题上,因为分段问题可能出现在这些函数指针体中。这些函数可能返回可能指向错误位置或null的字符指针。我认为这个问题有最小的、完整的和可验证的例子。我不想被否决。

你在Identity.c中初始化
this->id
,这样:
this->id=“”

strcat(this->id,b)
将尝试为字符串literal
写入只读存储器,但失败


顺便提一下,
b
在传递给
itoa时未初始化(str[i],b,10)这是另一个未定义行为的计数。

这不是我所说的。请尽量将代码缩小到仍然显示您的问题的最小部分。请记住,如果您想在
strcat
调用中将指针用作目的地,指针需要实际指向您可以写入数据的某个地方。哦,什么是
scanf(“%s”,…)
需要输入?变量
输入是什么?
“%s”
格式和
输入是否匹配?谢谢。我将这些结构放在我的问题上,因为分段问题可能出现在这些函数指针体中。(我从这些函数指针收集随机数据(字符指针)。四种可能:(1)您正在调用
strcat(a,b)
其中
a
b
为空指针;(2) 您正在调用
strcat(a,b)
,其中
a
b
是垃圾值(不指向任何地方);(3)
b
指向不是正确的以null结尾的字符串的字符;(4)
a
指向一个内存区域,该区域不够大,无法容纳连接的字符串。(4在这种情况下不太可能。)请阅读本页底部链接的关于“调试小程序”的页面。另外,请考虑中的“minimal”一词。非常感谢,为什么我不能将未初始化的字符指针传递给itoa()函数?@3code:不,您必须传递一个指向数组的指针,该数组必须足够大,才能将第一个参数的转换值作为C字符串接收,包括空终止符。@3code:还要处理您的演示文稿:代码错误严重。看看我如何重新格式化你的文章,使之可读。它还被认为是糟糕的样式,并且非常容易在typedef后面隐藏指针。你似乎没有掌握指针和数组的概念,和一个同事一起研究,正确地编写C代码是困难的,但却是必须的。最后,避免使用C++关键字,如<代码>这个< /代码>作为变量名,这对读者来说是混淆的。malloc(sizeof(char)*(12));itoa(str[i],b,10);'我用这种方法初始化b。如何用空终止符初始化b?@3code:
itoa
在它构造的字符串末尾设置空终止符。调用
itoa()
后,您可以使用
this->id=b将
b
存储到
this->id