?
Solved

C Program works with Turbu C++ editor, but not with a UNIX machine

Posted on 2004-11-08
1
Medium Priority
?
232 Views
Last Modified: 2010-04-15
Please see the bellow codes, and try to execute the llTest code on a Unix machine . . .

I compiled llTest on the "Turbo C++" and it works successfully, BUT when I  changed the extension from llTest.cpp to llTest.c, the compiler of the Unix machine gave me a lot of errors . . .

Please notify me with the solution to this problem since it is a major part of an Operating Course Project . . .

Thanks in advanced . . .

This is the header code:

/*                                               */
/* queue.c                                          */
/* Demo of dynamic data structures in C                      */

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

#define FALSE 0
#define NULL 0

struct listelement{
    int     dataitem;
    listelement *link;
};

listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);

listelement * AddItem (listelement * listpointer, int data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
      while (listpointer -> link != NULL)
          listpointer = listpointer -> link;
      listelement  *new_lp = (struct listelement *)malloc(sizeof(struct listelement));
      listpointer -> link = new_lp;
      listpointer = listpointer -> link;
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return lp;
    }
    else {
      listpointer = (struct listelement  *) malloc (sizeof (listelement));
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return listpointer;
    }
}

listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
}

void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
      printf ("queue is empty!\n");
    else
      while (listpointer != NULL) {
          printf ("%d\t", listpointer -> dataitem);
          listpointer = listpointer -> link;
      }
    printf ("\n");
}

void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
      listpointer = RemoveItem (listpointer);
    }
}

This is the code:

/*                                               */
/* queue.c                                          */
/* Demo of dynamic data structures in C                      */

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

main () {
    listelement listmember, *listpointer;
    int     data,
            choice;

    listpointer = NULL;

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);

            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);

            PrintQueue (listpointer);
             
            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                {
                listpointer = RemoveItem (listpointer);
                PrintQueue (listpointer);
            }

            PrintQueue (listpointer);
    ClearQueue (listpointer);

    return(0);
}
0
Comment
Question by:zaaki
1 Comment
 
LVL 11

Accepted Solution

by:
cjjclifford earned 150 total points
ID: 12524588
the first thing I see is that you define a structure "listelement" but don't refer to it anywhere as a struct, either change references to it from "listelement" to "struct listelement" or create a typedef of the struct:

e.g. suggestion 1 (struct)

struct listelement{
    int     dataitem;
    struct listelement *link;
};

struct listelement * AddItem (struct listelement * listpointer, int data);
struct listelement * RemoveItem (struct listelement * listpointer);
void PrintQueue (struct listelement * listpointer);
void ClearQueue (struct listelement * listpointer);


e.g. suggestion 2 (typedef)

typedef struct listelement{
    int     dataitem;
    listelement *link;
} listelement;

listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question