我正在做一个项目,其中我需要打印搜索的 struct ,但当我打印时,它进行第一次打印,然后退出运行,并出现以下错误: error个 我搜索了错误,这是动态内存的正常问题,但这不应该是我的情况,因为我只打印Wht在那里,我没有增加任何东西,根据我的知识.

打印功能:

void printCompany(Companies companies, int i) {
    printf("\nNif: %d", companies.companies[i].Nif);
    printf("\nName: %s", companies.companies[i].name);
    printf("\nCategory: %s", companies.companies[i].categories);
}

struct :

#define COMPANY_INITIAL_SIZE 20
#define SEARCHES_INITIAL_SIZE 20
#define ACTIVITIES_INITIAL_SIZE 20

#define TEXT_MAX_SIZE 200
#define NAME_MAX_SIZE 50
#define EMAIL_MAX_SIZE 50
#define TITLE_MAX_SIZE 50
#define STREET_MAX_SIZE 70
#define LOCATION_MAX_SIZE 70

#define NIF_MIN_SIZE 100000000
#define NIF_MAX_SIZE 999999999

    enum category {
        microCompany = 1, pmeCompany, macroCompany
    };

    enum state {
        inactive, active
    };

    enum searchCamp {
        nif, businessArea, nameLocation
    };

    typedef struct {
        char title[TITLE_MAX_SIZE];
        char text[TEXT_MAX_SIZE];
        char userName[NAME_MAX_SIZE];
        char userEmail[EMAIL_MAX_SIZE];
    } Coment;

    typedef struct {
        int nota;
        char userName[NAME_MAX_SIZE];
        char userEmail[EMAIL_MAX_SIZE];
    } Rating;

    typedef struct {
        int Nif;
        char name[NAME_MAX_SIZE];
        enum category categories;
        int businessAreaCode;
        enum state states;
        char street[STREET_MAX_SIZE];
        char location[LOCATION_MAX_SIZE];
        int postalCode;
        int nComents;
        Coment coments;
        int nRating;
        Rating ratings;
    } Company;

    typedef struct {
        int counter, size;
        Company *companies;
    } Companies;

    void freeCompanies(Companies *);
    void loadCompanyFile(Companies *, char*);
    void saveCompanyFile(Companies *, char *);
    void insertCompanies(Companies*);
    void expandCompanySize(Companies *);
    void searchNifCompanies(Companies);
    //void printCompany(Company);
    void printCompany(Companies,int);

function that calls the 打印功能:

/**
 * @brief This function compares the element 
 * @param companies The struct Companies.
 * @param nif The unique value of a company
 * @return if sucesseful returns the index of the company found, if  unsucesseful returns -1.
 */
int searchNifCompany(Companies companies, int nif) {
    int i;
    for (i = 0; i < companies.counter; i++) {
        if (companies.companies[i].Nif == nif) {

            return i;
        }
    }

    return -1;
}

/**
 * @brief This function 
 * @param *companies The pointer of the struct Companies.
 * @return 
 */
void searchNifCompanies(Companies companies) {
    int company, nif;

    nif = getInt(NIF_MIN_SIZE, NIF_MAX_SIZE, "\nNIF[9 digits]: ");
    company = searchNifCompany(companies, nif);

    if (company != -1) {
        //printCompany(companies.companies[company]);
        printCompany(companies, company);
    } else {

        puts("The company doesn't exist.");
    }
}

it gives me the same result even i manualy tell to print the struct :

Companies companies;

int main() {
    
    loadCompanyFile(&companies, COMPANIES_FILE_NAME);

    mainMenu();
    
    printf("\nNif: %d", companies.companies[1].Nif);
    printf("\nName: %s", companies.companies[1].name);
    printf("\nCategory: %s", companies.companies[1].categories);
    
    saveCompanyFile(&companies, COMPANIES_FILE_NAME);
    freeCompanies(&companies);
    return (EXIT_SUCCESS);
}

推荐答案

你应该得到一些关于printf的错误参数类型的编译器警告:

printf("\nCategory: %s", companies.companies[i].categories);

您传递的是整数值,而不是字符串中第一个字符的地址.

这是在你的printf功能上作弊.虽然您promise 提供字符串的地址(通过使用%s格式说明符),但您传递的值不是有效的地址.并且其大小也可能小于地址值(可能是32位对64位).

printf依赖于你遵守你的promise ,并按照你所说的那样接受这个价值.

这将导致非法内存访问触发堆栈转储.

始终听取来自编译器的警告.如果没有收到消息,请提高警告级别.

对于GCC或Clang,使用-Wall -Wextra -pedantic

顺便说一句: 您是否希望打印用于命名这enum个值的类似microCompany的文本?enums不是这样工作的.它们只是指定的数字.用于值的标识符在程序中不以字符串形式存在.

C++相关问答推荐

带双指针的2D数组

为什么PLT表中没有push指令?

如果实际的syscall是CLONE(),那么为什么strace接受fork()呢?

如何将不同长度的位转换成字节数组?

为什么在C中进行大量的位移位?

将整数的.csv文件解析为C语言中的二维数组

非常大的数组的大小

文件权限为0666,但即使以超级用户身份也无法打开

为什么我不能只在内存地址中添加一个int来寻址任何数组?

如何用c语言修改shadow文件hash部分(编程)?

<;unistd.h>;和<;sys/unistd.h>;之间有什么区别?

处理来自浏览器的HTTP请求

GCC奇怪的行为,有fork 和印花,有换行符和不换行符

为四维数组中的Dim-1和Dim-3重新分配空间

判断系统命令返回值的正确方法

`预期说明符-限定符-列表在‘(三元运算符中的’token`‘之前

可以对两种 struct 类型中的任何一种进行操作的C函数

在我的第一个C语言中观察到的错误';你好世界';程序

即使客户端不发送数据,也会发生UNIX套接字读取

DennisM.Ritchie的C编程语言一书中关于二进制搜索的代码出现错误?