2

I did my whole project in NetBeans on my mac and it works fine. Then I go to put it into terminal to submit it on our ssh server for school and its giving me a bunch of compilation errors. Did netbeans compile in c++ or something? I'm new to c so any help is certainly appreciated. Here's the code.

/* 
 * File:   LinkedList.c
 * Author: dpiganell
 *
 * Created on October 17, 2011, 2:31 PM
 */

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

struct element{
    int i;
    struct element *next;
};

void insert(struct element**, struct element*);
void purge (struct element*, struct element*);
void printList(struct element*);
void printListBackwards(struct element*);

struct element *h;

int main() 
{
    // HEAD
    h = (element *) malloc(sizeof(element));
    h = NULL;

    // NEW VALUE
    struct element *n = NULL;

    // POINTER TO THE POINTER OF HEAD
    struct element **headPointer;
    headPointer = (element **) malloc(sizeof(element));

    int a;
    a = 1;
    while(a >= 0)
    {
        n = (element *) malloc(sizeof(element));

        printf("\nPlease enter an integer value: ");
        scanf("%d", &a);

        if(a >= 0)
        {
            n->i = a;
            n->next = NULL;
            headPointer = &h;

            insert(headPointer, n);

        n = n++;


      }
        printList(h);
        printListBackwards(h);
    }

    return 0;
}


void insert(struct element **head, struct element *n)
{
    element *nptr, *pptr, *cptr;
    // NEW POINTER, PREVIOUS POINTER, CURRENT POINTER

    nptr = (element *) malloc(sizeof(element));

    int purged;
    purged = -1;
    if(nptr != NULL)
    {
        nptr->i = n->i;
        nptr->next = NULL;

        cptr = *head;
        pptr = NULL;
    }

    while(cptr != NULL &&  n->i >= cptr->i)
    {
        if(cptr->i == n->i && purged != 1)
        {
            purged = 1;
            purge(cptr, pptr);
        }
        else
        {
            pptr = cptr;
            cptr = cptr->next;
        }
    }

    if(pptr == NULL && purged < 0)
    {
        nptr->next = *head;
        *head = nptr;
    }
    else if(purged < 0)
    {
        pptr->next = nptr;
        nptr->next = cptr;
    }
}


void purge(struct element *current, struct element *predecessor)
{
    element *ptr = (element *) malloc(sizeof(element));

    // ERASING THE HEAD
    if(predecessor == NULL)
    {
        if(current->next == NULL)
        {
            current->next = NULL;
            current->i = NULL;
            current = NULL;
            free(current);
            h = NULL;
        }
        else
             memcpy(current, current->next, sizeof(element));
    }

    // ERASING THE TAIL
    else if(current->next == NULL)
    {
        current->i = NULL;
        free(current->next);
        free(current);
        predecessor->next = NULL;
    }

    // ERASING ANYTHING IN THE MIDDLE OF THE LIST
    else
    {
        ptr = current->next; 
        predecessor->next = ptr;
        current->i = NULL;
        free(current->next);
    }
}

void printList(struct element *head)
{
    if(head == NULL)
        printf("The list is empty.");
    else
    {
        struct element *ptr;
        ptr = (element*) malloc(sizeof(element));    
        ptr = head;  

        int a;
        a = 1;
        printf("Forwards: ");
        while(a > 0)
        {
            printf("%d ", ptr->i);
            if((ptr->next) == NULL)
                a = -1;
            else
                ptr = ptr->next;

        }
    }
}

void printListBackwards(struct element *ptr)
{
    if(ptr == NULL)
    {
        // DO NOTHING BECAUSE IT WILL BE PRINTED ALREADY THAT IT IS EMPTIED
    }
    else
    {
        element *cptr = (element *) malloc(sizeof(element));
        cptr = ptr;
        if(ptr->next == NULL)
            printf("\nBackwards: %d ", ptr->i);
        else
        {
            printListBackwards(ptr->next);
            printf("%d ", ptr->i);
        }
    }
}

Also, here are the errors. It works fine and compiles fine in netbeans.

LinkedList.c:14: warning: useless keyword or type name in empty declaration
LinkedList.c: In function `main':
LinkedList.c:26: error: `element' undeclared (first use in this function)
LinkedList.c:26: error: (Each undeclared identifier is reported only once
LinkedList.c:26: error: for each function it appears in.)
LinkedList.c:26: error: syntax error before ')' token
LinkedList.c:34: error: syntax error before ')' token
LinkedList.c:40: error: syntax error before ')' token
LinkedList.c: In function `insert':
LinkedList.c:65: error: `element' undeclared (first use in this function)
LinkedList.c:65: error: `nptr' undeclared (first use in this function)
LinkedList.c:65: error: `pptr' undeclared (first use in this function)
LinkedList.c:65: error: `cptr' undeclared (first use in this function)
LinkedList.c:68: error: syntax error before ')' token
LinkedList.c: In function `purge':
LinkedList.c:110: error: `element' undeclared (first use in this function)
LinkedList.c:110: error: `ptr' undeclared (first use in this function)
LinkedList.c:110: error: syntax error before ')' token
LinkedList.c: In function `printList':
LinkedList.c:153: error: `element' undeclared (first use in this function)
LinkedList.c:153: error: syntax error before ')' token
LinkedList.c: In function `printListBackwards':
LinkedList.c:179: error: `element' undeclared (first use in this function)
LinkedList.c:179: error: `cptr' undeclared (first use in this function)
LinkedList.c:179: error: syntax error before ')' token
8
  • You're trying to use a type that you didn't define. Commented Oct 20, 2011 at 23:15
  • Are you sure you're showing us the exact code that gave you those errors? When I compile it, the first error I see is that element is undeclared (in C, that type's name is struct element). I can't reproduce the errors you're seeing in C++ either. Commented Oct 20, 2011 at 23:15
  • @KeithThompson The errors aren't in C++. The question was answered though thanks for the help. I compiled it in netbeans in a c++ compiler and it worked. But when I used gcc it didn't work just to clear things up. Thanks everyone. Commented Oct 20, 2011 at 23:18
  • @iSelkiies i guess it is time to accept an answer. Commented Oct 20, 2011 at 23:20
  • 1
    Keith knows. But the program is supposed to be C, not c++. Also: you malloc() too much. Commented Oct 20, 2011 at 23:20

5 Answers 5

5

In C, this definition

struct element{
    int i;
    struct element *next;
};

has to be referred to as struct element. You get that right in some places, not all. A known idiom is:

typedef struct element { 
    int i;
    struct element *next;
} element;

Which typedefs element as struct element so you don't need to place struct in front of the type. This was so common that C++ kind of "does this for you" so struct is no longer needed.

Sign up to request clarification or add additional context in comments.

Comments

4

It's C, you cannot use bare element, it's struct element.

Comments

1

Your issues are related to how you declare your struct element vs. how you use it.

typedef struct element{
    int i;
    struct element *next;
} element;

will give you more satisfactory results.

Alternately, you can leave your struct as it is, wherever you use a bare element, instead write struct element.

BTW:

This isn't related to your exact problem, but I thought I would share (as my compiler gave me a warning about it); you sometimes write:

current->i = NULL;

In this case, i is an integer. In C, NULL is traditionally used only for pointer types. So as not to confuse, you should either change the assignment to:

current->i = 0;

...or, if i actually is meant to point to memory somewhere (which given the context above, I presume it is not), declare it as a pointer type.

Comments

1

You need to typedef your struct if you're going to use it that way. Otherwise you need to use the struct keyword everywhere. Check documentation

So you want to say:

typedef struct element {
   int i;
   element *next;
} element;

when you declare element. Otherwise the compiler doesn't know that element is a structure every time you use it.

Comments

0
h = (element *) malloc(sizeof(element));
h = NULL;
headPointer = (element **) malloc(sizeof(element));

This is your first error. element is a struct tag, not a typedef. (this has been handled by the others.)

in the loop:

headPointer = &h;

Both pointers have been assigned (malloc()d) values. By re-assigning them, you lose the stuff what was previously malloc()d.

The 'headPointer' stuff is a good idea. It is a pointer that can point to other pointers. You can use it to let a called function alter the the value of on of the callers pointers. But your usage is not right. Normally the caller should do:

struct clown *pipo=NULL;
...
my_function( &pipo);

Inside the function, the caller's pointer can than be altered:

void my_function (struct clown **ppp) {
   ...
   *ppp = malloc (sizeof (struct clown));
   ...
 }

After the function returned, the caller can use the pointer, and it will hopefully point at the new clown. That's all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.