在C程序中获得奇怪的错误

时间:2016-07-16 18:19:03

标签: c visual-studio-2012

此代码工作正常,但在某些时候我开始收到这些错误。 任何帮助赞赏。这些错误令人困惑,因为它们甚至看起来与真实的行号无关,例如:如果错误说某些变量未在某行上声明,那么我就去了那一行,那个变量就不存在了。

这里是header和c文件,然后是最后的主文件。

头:

#ifndef G_LINKED_LIST  /* Include guard */
#define G_LINKED_LIST

//
// Linked list node
// Containes data and a pointer to the next node in the list.
//
struct LL_node {
    int data;
    struct LL_node* next;
};

// Kill program and print error message
void die(const char *message);

// Get linked list length, number of nodes in the list.
int LL_Length(struct LL_node* head);

// Create a demo linked list and return pointer to it.
struct LL_node* BuildTestList(void);

// Insert node at the front of linked list.
void InsertAtFront(struct LL_node** headRef, int data);

//  Dump linked list contents.
void PrintList(struct LL_node* headPtr);

#endif

c file

//
//
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "GLinkedList.h"

// Print linked list contents
void PrintList(struct LL_node* headPtr)
{
    // Get pointer to the start of the linked list.
    struct LL_node *temp = headPtr;

    printf("About to dump linked list contents\n");

    // Iterate the linked list.
    while (temp != NULL)
    {
        printf("List node value: %d\n",temp->data);
        temp = temp->next;
    }
}

// Kill program and print error message
void die(const char *message)
{
    if(errno) {
        perror(message);
    } else {
        printf("ERROR: %s\n", message);
    }

    exit(1);
}

//
//  Given a linked list head pointer, compute
//  and return the number of nodes in the list.
//
int LL_Length(struct LL_node* head) {

    struct LL_node* current = head;

    int count = 0;
    while (current != NULL) {
        count++;
        current = current->next;
    }

    return count;

}

//
// Build the list {1, 2, 3} in the heap and store
// its head pointer in a local stack variable.
// Returns the head pointer to the caller.
//
struct LL_node* BuildTestList() {

    //
    // Create three nodes on the heap : 
    //

    struct LL_node* first;
    struct LL_node* second;
    struct LL_node* third;

    first = malloc(sizeof(struct LL_node));
    if(first == NULL)
        die("Memory allocation failure.");

    second = malloc(sizeof(struct LL_node));
    if(second == NULL)
        die("Memory allocation failure.");


    third = malloc(sizeof(struct LL_node));
    if(third == NULL)
        die("Memory allocation failure.");


    //
    // Chain them
    //

    first->data = 1; 
    first->next = second; 

    second->data = 2; 
    second->next = third;

    third->data = 3; 
    third->next = NULL;

    // Return pointer to first element
    return first;
}


//
// Takes a list and a data value.
// Creates a new link with the given data and pushes
// it onto the front of the list.
// The list is not passed in by its head pointer.
// Instead the list is passed in as a "reference" pointer
// to the head pointer -- this allows us
// to modify the caller's memory.
//
void InsertAtFront(struct LL_node** headRef, int data) 
{
    struct LL_node* newNode = malloc(sizeof(struct LL_node));

    if(newNode==NULL)
        die("Memory allocation failure.");

    newNode->data = data;
    newNode->next = *headRef; // The '*' to dereferences back to the real head
    *headRef = newNode; // ditto
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "GLinkedList.h"


int main()
{
    int dummy;

    // Create a test linked list
    struct LL_node *headPtr;
    headPtr = BuildTestList();

    PrintList(headPtr);

    // Try to insert element at front of the list
    InsertAtFront(&headPtr, 55); 

    // Try to insert element at front of the list
    InsertAtFront(&headPtr, 33); 

    PrintList(headPtr);

    // wait
    scanf("%d",&dummy);
    return 0; 
}

误差修改:

------ Rebuild All started: Project: LinkedLists, Configuration: Debug Win32 ------
  main.c
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\main.c(25): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
          c:\program files\microsoft visual studio 11.0\vc\include\stdio.h(290) : see declaration of 'scanf'
  GLinkedList.c
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(21): warning C4210: nonstandard extension used : function given file scope
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(21): error C2143: syntax error : missing ';' before '{'
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(23): error C2065: 'message' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(23): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(23): warning C4024: 'perror' : different types for formal and actual parameter 1
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(25): error C2065: 'message' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(35): error C2143: syntax error : missing ';' before 'type'
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(39): error C2143: syntax error : missing ';' before 'type'
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(40): error C2065: 'current' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(40): warning C4047: '!=' : 'int' differs in levels of indirection from 'void *'
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(41): error C2065: 'count' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(42): error C2065: 'current' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(42): error C2223: left of '->next' must point to struct/union
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(45): error C2065: 'count' : undeclared identifier
c:\users\g\documents\visual studio 2012\projects\linkedlists\linkedlists\glinkedlist.c(45): warning C4098: 'PrintList' : 'void' function returning a value
  Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

任何帮助,因为我真的迷路了。这是VS 2012

0 个答案:

没有答案