C 程序工作,但无法理解警告

C 程序工作,但无法理解警告,c,pointers,struct,warnings,C,Pointers,Struct,Warnings,我正在学习C语言,无法理解这些警告 这些警告有什么区别吗 如果我收到这些警告,这是否意味着我编码错误 代码如下: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #include "addSorted.h" #include "freeLinks.h" #include "myStruct.h" #include "main.h" struct

我正在学习C语言,无法理解这些警告
这些警告有什么区别吗
如果我收到这些警告,这是否意味着我编码错误

代码如下:

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

#include "addSorted.h"
#include "freeLinks.h"
#include "myStruct.h"
#include "main.h"

struct Node* getNewNode(char *name)
{
    struct Node *newNode = malloc(sizeof (struct Node));

    strcpy(newNode->name, name);
    newNode->prev = NULL;
    newNode->next = NULL;

    return newNode;
}

char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while (isspace(*str)) str++;

    if (*str == 0) // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while (end > str && isspace(*end)) end--;

    // Write new null terminator
    *(end + 1) = 0;

    return str;
}

void deleteValue(struct Node **head, char *name)
{
    struct Node* current = *head;

    /* Find name */
    while (current != NULL)
    {
        if (strcmp(current->name, name) == 0)
        {
            break;
        }
        else if (strcmp(current->name, name) > 0)
        {
            printf("\nError: %s is not in the list\n", name);
            return;
        }
        else
        {
            current = current->next;
        }
    }

    if (current == NULL)
    {
        printf("\nError: %s is not in the list\n", name);
    }
    else
    { /* current != NULL */
        if (current->prev == NULL && current->next == NULL)
        {
            /* Only node in head */
            *head = NULL;
        }
        else if (current->prev == NULL)
        {
            /* First node in head */
            *head = current->next;
            (*head)->prev = NULL;
        }
        else if (current->next == NULL)
        {
            /* Last node in head */
            current->prev->next = NULL;
        }
        else
        {
            /* Node in middle of head */
            current->prev->next = current->next;
            current->next->prev = current->prev;
        }
    }
    free(current);
    current = NULL;
}

void printHead(struct Node *head)
{
    printf("\n****Printing Head First****\n");
    struct Node *temp = head;
    while (temp != NULL)
    {
        printf("%s \n", temp->name);
        temp = temp->next;
    }
}

void printTail(struct Node *head)
{
    struct Node* temp = head;
    if (temp == NULL) return; // empty list, exit
    // Going to last Node
    while (temp->next != NULL)
    {
        temp = temp->next;
    }
    // Traversing backward using prev pointer
    printf("\n****Printing Tail First****\n");
    while (temp != NULL)
    {
        printf("%s \n", temp->name);
        temp = temp->prev;
    }
    printf("\n");
}

int main(void)
{
    struct Node *head = NULL, *temp = NULL;
    FILE * fp;
    char * line = NULL, *name = NULL, *operator = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("data.txt", "r");
    if (fp == NULL)
    {
        exit(EXIT_FAILURE);
    }
    while ((read = getline(&line, &len, fp)) != -1)
    {
        name = strtok(line, " ");
        operator = strtok(NULL, "\n");
        name = trimwhitespace(name);

        if (strncmp(operator, "a", 1) == 0)
        {
            temp = getNewNode(name);
            addSorted(&head, temp);
        }
        else if (strncmp(operator, "d", 1) == 0)
        {
            deleteValue(&head, name);
        }
    }

    fclose(fp);
    if (line)
    {
        free(line);
    }

    // Print Forward
    printHead(head);

    // Print Reverse
    printTail(head);

    // Free Links
    freeLinks(head);
}
addSorted.h

#ifndef MYSTRUCT_H
#define MYSTRUCT_H

struct Node
{
    char name[42];
    struct Node *next;
    struct Node *prev;
};

#endif  /* MYSTRUCT_H */
#ifndef ADDSORTED_H
#define ADDSORTED_H

void addSorted(struct Node **head, struct Node *newNode);

#endif  /* ADDSORTED_H */
addSorted
实现:

#include "myStruct.h"
#include <stdio.h>
#include <string.h>

void addSorted(struct Node **head, struct Node *newNode)
{
    struct Node *current;
    // Special case for the head end 
    if ((*head == NULL) || strcmp((*head)->name, newNode->name) >= 0)
    {
        newNode->next = *head;
        if (*head != NULL) (*head)->prev = newNode;
        *head = newNode;
    }
    else
    {
        // Locate the Node before the point of insertion 
        current = *head;
        while (current->next != NULL &&
               strcmp(current->next->name, newNode->name) <= 0)
        {
            current = current->next;
        }
        newNode->next = current->next;
        if (current->next != NULL)current->next->prev = newNode;
        current->next = newNode;
        newNode->prev = current;
    }
}
以及
自由链接
文件:

#include "myStruct.h"
#include<stdio.h>

void freeLinks(struct Node *head)
{
    struct Node* current = head;
    while (current != NULL)
    {
        struct Node* next = current->next;
        free(current);
        current = next;
    }
    current = NULL;
}
您需要
#在头文件中包含“myStruct.h”
,该头文件具有引用节点的函数声明。这包括“addSorted.h”和“freelinks.h”

此外,键入def结构可以节省大量时间:

    typedef struct { //struct fields
    }Node;  

将允许您将变量命名为type
Node
,而不是
struct Node

简短回答:是的,很可能你正在做一些不应该做的事情。警告是你的朋友。但是为什么传递相同类型的
struct节点*
也会产生警告呢。我愿意理解解决方案。我没有测试过任何东西,但我会将
#include“myStruct”
写入
Node
struct-tooth的头文件中
struct-Node
的声明必须在使用前出现。结构节点在哪里声明?
addSorted.h
需要
#包括“myStruct.h”
。否则同上。请不要键入每个结构。这通常是个坏主意,因为它会污染名称空间。编写
结构节点
有什么困难?
In file included from main.c:10:
./addSorted.h:12:23: warning: declaration of 'struct Node' will not be visible outside of this function [-Wvisibility]
void addSorted(struct Node **head, struct Node *newNode);
                      ^
In file included from main.c:11:
./freeLinks.h:12:23: warning: declaration of 'struct Node' will not be visible outside of this function [-Wvisibility]
void freeLinks(struct Node *head);
                      ^
main.c:153:23: warning: incompatible pointer types passing 'struct Node **' to parameter of type 'struct Node **' [-Wincompatible-pointer-types]
            addSorted(&head, temp);
                      ^~~~~
./addSorted.h:12:30: note: passing argument to parameter 'head' here
void addSorted(struct Node **head, struct Node *newNode);
                             ^
main.c:153:30: warning: incompatible pointer types passing 'struct Node *' to parameter of type 'struct Node *' [-Wincompatible-pointer-types]
            addSorted(&head, temp);
                             ^~~~
./addSorted.h:12:49: note: passing argument to parameter 'newNode' here
void addSorted(struct Node **head, struct Node *newNode);
                                                ^
main.c:174:15: warning: incompatible pointer types passing 'struct Node *' to parameter of type 'struct Node *' [-Wincompatible-pointer-types]
    freeLinks(head);
              ^~~~
./freeLinks.h:12:29: note: passing argument to parameter 'head' here
void freeLinks(struct Node *head);
                        ^
    typedef struct { //struct fields
    }Node;