C 控制台应用程序的编译不会返回错误,但也不会运行代码

问题描述 投票:0回答:1

两天前,我发布了这个问题,关于在控制台应用程序项目中组织多个 .c 和 .h 文件。我遵循了每个人的指导和建议,这引导我构建了这个项目:

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"
#include "search.h"

int main () {
    ...
    clear();
    ...
    search();
    ...
    return 0;
}

utils.c:

#include "utils.h"

void clear() {
    #ifdef _WIN32
    system("cls");
    #else
    system("clear");
    #endif
}

utils.h:

#ifndef UTILS_H
#define UTILS_H

#include <stdlib.h>

void clear();

#endif

结构.h:

#ifndef STRUCTS_H
#define STRUCTS_H

typedef struct {
    char *brand;
    float fee;
} Brand;

typedef struct {
    Brand brand;
    char *model;
    char *color;
    float price;
} Car;

#endif

搜索.c:

#include <search.h>

Car *search () {
    ...
    clear();
    ...
    Car cars[255];
    ...
    return cars;
}

搜索.h:

#ifndef SEARCH_H
#define SEARCH_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"

Car *search ();

#endif

这是项目文件夹中的项目设置:

这是我正在运行的编译命令:

gcc -o output main.c utils.c search.c

现在,过去两天主要花在修复终端在所有文件中出现的各种问题上出现的错误。今天,我终于成功修复了所有控制台错误,当我最终再次运行编译命令时......什么也没发生。

我真的再次迷失了,不知道我错过了什么。非常感谢任何帮助,并且一如既往,对于马虎的英语感到抱歉! (在截图中你可以看到我是巴西人)

c struct compilation console console-application
1个回答
0
投票

我尝试编译并运行你的代码,编译器立即抱怨你的本地“search.h”包含文件。

#include <search.h>

正如您其他问题参考中的答案所述,文件参考应该用双引号括起来。

    #include "search.h"

进行调整后,程序确实进行了编译,但编译器注册了一条有关尝试将本地结构数组值从函数返回到调用代码的警告。

/home/craig/C_Programs/Console/Cars/search.c|9|warning: function returns address of local variable [-Wreturn-local-addr]|

Car *search () {
    ...
    clear();
    ...
    Car cars[255];
    ...
    return cars;
}

我的猜测是,当你编译这组代码时,你也收到了类似的编译器警告。

该方法不会为您提供所需的结果,因为一旦退出函数,本地结构数组“cars”就会被销毁。如果要在函数中定义结构体数组,可以为结构体数组分配内存,分配一个指向该数组的指针,然后返回该指针。

考虑到这一点,以下是“搜索”功能和“主要”功能的重构版本,以提供思考。

#include "search.h"

Car *search (void)
{
    cleared();

    Car *cars = malloc(255 * sizeof(Car));  /* Allocate and pass back the pointer to the memory block   */

    cars[0].brand.brand = "Chevy";          /* Just initialized some sample data    */
    cars[0].color = "Candy Apple Red";

    return cars;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"
#include "search.h"

int main () {

    cleared();              /* Cleared the terminal         */

    Car * cars = search();  /* Set up the pointer to the structure array    */

    printf("Brand: %s, color: %s\n", cars[0].brand.brand, cars[0].color);   /* Simple test output   */

    return 0;
}

需要注意的一些关键事项。

  • 本地“include”文件已通过用双引号引起来进行更改。
  • 使用“malloc”函数为 car 结构分配内存,然后将分配的内存块的指针返回给调用程序,而不是使用“Car cars[255]”定义它。

添加一些测试代码以提供一些说明性数据,以下是终端的输出。

Brand: Chevy, color: Candy Apple Red

尝试一下重构。需要注意的关键是要了解所有编译器警告以及任何编译器错误。警告通常表明,如果不解决,程序将在运行时出现一些未定义的行为。此外,您可能想深入研究其他“C”教程材料,因为它涉及定义数组等元素并为它们分配内存以及此类内存分配的范围。

© www.soinside.com 2019 - 2024. All rights reserved.