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

时间:2015-07-12 21:24:15

标签: 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 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);
}

myStruct.h

#ifndef MYSTRUCT_H
#define MYSTRUCT_H

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

#endif  /* MYSTRUCT_H */

addSorted.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;
    }
}

freeLinks标题:

#ifndef FREELINKS_H
#define FREELINKS_H

void freeLinks(struct Node *head);

#endif  /* FREELINKS_H */

freeLinks文件:

#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;
}

我收到这些警告:

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);
                        ^

1 个答案:

答案 0 :(得分:1)

您的头文件中需要writecb,其中包含对Node的引用的函数声明。这包括“addSorted.h”和“freelinks.h”。

此外,您可以通过typedef结构来节省大量时间:

#include "myStruct.h"

将允许您将变量命名为 typedef struct { //struct fields }Node; 而不是Node