Visual Studio Code 中的警告

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

当我运行代码时,它返回许多警告,但我仍然有预期的输出,没有行错误。我考虑是否必须修复我的代码。

这是我用于本练习的代码: 假设一列火车有 N 节车厢连接在一起 例如 car1、car2、car3、...、carN 。一列火车可以被认为是一个列表,每节车厢对应于这个列表中的一个节点。



#include <stdio.h>
#include <stdlib.h>

// Structure for cars of train
typedef struct _Car {
    int capacity;
    char id;
    int passengers;
    struct Car *pnext;
}Car;

// Structure for train
typedef struct _Train {
    int size;
    Car *pHead;
    Car *pTail;
}Train;

// Initialize the train with no element
void initTrain(Train *train) {
    train->size = 0;
    train->pHead = NULL;
    train->pTail = NULL;
}

// Initialize car
Car *initCar(char id, int capacity, int passengers) {
    Car *car = (Car*)malloc(sizeof *car);
    car->capacity = capacity;
    car->id = id;
    car->passengers = passengers;
    car->pnext = NULL;
    return car;
}

// Find a car in the train by its ID
Car *findCarById(Train *train, char id) {
    Car *ptr = train->pHead;
    
    while (ptr != NULL) {
        if (ptr->id == id) {
            return ptr;
        }
        ptr = ptr->pnext;
    }
    
    // Car with the specified ID not found
    return NULL;
}

// Verify whether a list is not empty
int isEmpty(Train *train) {
    return (train->size == 0);
}

// Add new car: 2 cases
// ptr points to a specific car
void addCar(Train *train, char id, int capacity, int passengers, Car *ptr) 
{
    Car *newcar = initCar(id, capacity, passengers);

    // If the train list is empty -> new car will be added to the list head
    if (isEmpty(train)) {
        train->size++;
        train->pHead = newcar;
        train->pTail = newcar;
    }
    else {
    // New car is linked after a specific car (ptr)
        if (ptr == NULL) { 
            newcar->pnext = train->pHead;
            train->pHead = newcar;
        }
        else {
            newcar->pnext = ptr->pnext;
            ptr->pnext = newcar;
            if (ptr == train->pTail) {
                train->pTail = newcar;
            }
        }
    train->size++;
    }
}

// Remove empty cars: 2 cases
void removeEmptycar(Train *train, int val) 
{
    // If the first car is empty and removed
    Car *ptr = train->pHead;
    if (ptr->passengers == val) {
        train->pHead = ptr->pnext;
        free(ptr);
        train->size--;
        return;
    }

    // If any car is removed, a new link is created between the previous car and the next car
    Car *q = ptr->pnext;
    int count = 1;
    while ((ptr->passengers != val) && (count < trainLength(train))) {
        q = ptr; // update q is current node so that q always points to the previous node of ptr
        ptr = ptr->pnext;
    }
    if (ptr != NULL) { // check if the node containing the value was found
        q->pnext = ptr->pnext;
        train->size--;
        free(ptr);
    }
}

// Length of train
int trainLength(Train *train) {
    return train->size;
}

// Display all cars's information
void display(Train *train) {
    Car *ptr = train->pHead;
    printf("Information:\n");
    while (ptr != NULL) {
        printf("Car: %c - Capacity: %d - Passengers: %d\n",ptr->id,ptr->capacity,ptr->passengers);
        ptr = ptr->pnext;
    }
}

int main() 
{
    Train train;
    initTrain(&train);

    addCar(&train, 'A', 50, 34, NULL);
    addCar(&train, 'B', 45, 39, NULL);
    addCar(&train, 'C', 60, 0, NULL);
    printf("Before:\n");
    display(&train);

    Car *newcar = findCarById(&train, 'A'); // find specific car 'A' to use it as a reference
    if (newcar != NULL) {
        addCar(&train, 'E', 8, 6, newcar); // add car 'E' after the car 'A'
    }
    printf("After add new car:\n");
    display(&train);


    removeEmptycar(&train, 0);
    printf("After remove empty car:\n");
    display(&train);


    return 0;
}
c visual-studio-code output warnings
1个回答
0
投票

复制代码然后编译代码,我确实也遇到了有关“Car *”引用的编译器警告。主要问题是您的代码试图使用实际上尚未定义的结构中的指针。

// Structure for cars of train
typedef struct _Car
{
    int capacity;
    char id;
    int passengers;
    struct Car *pnext;             /* In effect, "Car" has not been defined at this point   */
} Car;

因此在这种情况下,引用需要指向初始定义标签“_Car”。

// Structure for cars of train
typedef struct _Car
{
    int capacity;
    char id;
    int passengers;
    struct _Car *pnext;             /* In effect, "Car" has not been defined at this point   */
} Car;

进行一点重构后,编译仍然会导致隐式函数声明的问题,因为函数“trainLength()”被放置在引用它的另一个函数之后,但没有函数原型。

/home/craig/C_Programs/Console/Railroad/main.c|114|warning: implicit declaration of function ‘trainLength’ [-Wimplicit-function-declaration]|

因此该函数在使用之前已被移至上位,并且程序编译时没有错误或警告。

// Length of train
int trainLength(Train *train)
{
    return train->size;
}

// Remove empty cars: 2 cases
void removeEmptycar(Train *train, int val)
{

因此,当变量、函数、指针或其他对象被定义并可供引用时要小心。

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