提问人:ahnherin092 提问时间:11/7/2023 最后编辑:dbushahnherin092 更新时间:11/8/2023 访问量:70
Visual Studio Code 中的警告
Warning in Visual Studio Code
问:
当我运行我的代码时,它返回许多警告,但我仍然有预期的输出,没有行错误。我考虑是否必须修复我的代码。
这是我本练习的代码: 假设一列铁路列车有 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;
}
答:
1赞
NoDakker
11/8/2023
#1
复制你的代码,然后编译代码,我确实也去编译器警告“汽车*”引用。主要问题是您的代码试图在实际上尚未定义的结构中使用指针。
// 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)
{
因此,当变量、函数、指针或其他对象被定义并可供引用时,请小心。
评论
gcc ... -Werror ...