1

This code was working fine, but at some point I started getting these errors. Any help appreciated. The errors are confusing as they don't even to seem to relate to real line numbers, e.g. if error says some variable not declared on some line I go to that line, that variable isn't even there.

here is header and c file and then in the end main file.

header:

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

main

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

Erros:

------ 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 ==========

Any help appreciated as I am really lost. It is VS 2012

  • 1
    this kind of error is generally caused by incorrect syntax. A missing bracket or parenthesis, a missing quote, missing semi-colon or some other typo. The error is probably before line 21 of glinkedlist.c – Tibrogargan Jul 16 '16 at 18:23
  • @Tibrogargan After I remove PrintList function from project (e.g. header source and main) it compiles –  Jul 16 '16 at 18:25
  • @Tibrogargan someone please help, this is driving me mad –  Jul 16 '16 at 18:28
  • the code you've posted compiles cleanly for me in gcc. It's likely to be something in the IDE contributing to what you're seeing – Tibrogargan Jul 16 '16 at 18:35
  • 3
    There's nothing wrong with the code you've posted. I compiled it with clang and it compiles without any errors or warnings. So I assume that you messed up something in the project. I would start a new project, and copy the files into the new project. – user3386109 Jul 16 '16 at 18:36
  • 1
    I copy/pasted you files to my project and it was built without errors. Please attach your `.vcxproj` file – mvidelgauz Jul 16 '16 at 18:47
  • @mvidelgauz Please find here: http://www.filedropper.com/linkedlistsvcxproj_1 –  Jul 16 '16 at 18:50
  • You uploaded `LinkedLists.vcxproj**.filters**` file. Please attach project file – mvidelgauz Jul 16 '16 at 18:56
  • even better - zip and upload entire project folder. – mvidelgauz Jul 16 '16 at 19:06
  • @mvidelgauz I created new project copied code, typed PrintListfunction by hand from scratch now it compiles. thanks for help anyway –  Jul 16 '16 at 19:16
  • @mvidelgauz btw do you know if there is any reason creating temporary pointer current here: http://codepad.org/VR4rRObf ?? why not just use head? –  Jul 16 '16 at 19:17
  • No, up to my (rather limited) understanding `head` is allocated on stack and used only by individual function invocation so it can be freely altered by function. I can only speculate that _maybe_ on some platforms (for example on System/360) or calling conventions it _may_ come back to caller so altering it may cause undesired side effects. For that reason (particularly) there is `const` specifier which I often use in my function prototypes just to get things "better organized". But this is topic for a different discussion in a different place :) – mvidelgauz Jul 16 '16 at 19:31
  • @mvidelgauz Yes I also don'e see why temporary pointer is used inside function. they could directly operate on head. thanks for answer –  Jul 17 '16 at 08:58
  • IMHO, this is at least good coding style even if there are no pure technical reasons – mvidelgauz Jul 17 '16 at 09:05

0 Answers0