?
Solved

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

Posted on 2004-11-08
3
Medium Priority
?
447 Views
Last Modified: 2011-04-14
Good day . . .

Please see the bellow cods, 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 code:

/* Demo of dynamic data structures in C                      */

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

#define FALSE 0
#define NULL 0

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

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

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

    listpointer = NULL;
    do {
         Menu (&choice);
      switch (choice) {
          case 1:
            printf ("Enter data item value to add  ");
            scanf ("%d", &data);
            listpointer = AddItem (listpointer, data);
            break;
          case 2:
            if (listpointer == NULL)
                printf ("Queue empty!\n");
            else
                listpointer = RemoveItem (listpointer);
            break;
          case 3:
            PrintQueue (listpointer);
            break;

          case 4:
            break;

          default:
            printf ("Invalid menu choice - try again\n");
            break;
      }
    } while (choice != 4);
    ClearQueue (listpointer);

    return(0);
}                        /* main */

void Menu (int *choice) {

    char    local;

    printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\
\t3 to print queue\n\t4 to quit\n");
    do {
      local = getchar ();
      if ((isdigit (local) == FALSE) && (local != '\n')) {
          printf ("\nyou must enter an integer.\n");
          printf ("Enter 1 to add, 2 to remove, 3 to print, 4 to
quit\n");
      }
    } while (isdigit ((unsigned char) local) == FALSE);
    *choice = (int) local - '0';
}

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 list of semilar errors:

test2.c:16: warning: useless keyword or type name in empty declaration
test2.c:19: parse error before `*'
test2.c:19: parse error before `*'
test2.c:19: warning: data definition has no type or storage class
test2.c:20: parse error before `*'
test2.c:20: parse error before `*'
test2.c:20: warning: data definition has no type or storage class
test2.c:21: parse error before `*'
test2.c:22: parse error before `*'
test2.c: In function `main':
test2.c:25: `listelement' undeclared (first use in this function)
test2.c:25: (Each undeclared identifier is reported only once
test2.c:25: for each function it appears in.)
test2.c:25: parse error before `listmember'
test2.c:29: `listpointer' undeclared (first use in this function)
test2.c: At top level:
test2.c:78: parse error before `*'
test2.c:78: parse error before `*'
test2.c: In function `AddItem':
test2.c:80: `listelement' undeclared (first use in this function)
test2.c:80: `lp' undeclared (first use in this function)
test2.c:80: `listpointer' undeclared (first use in this function)
test2.c:85: `new_lp' undeclared (first use in this function)
test2.c:90: `data' undeclared (first use in this function)
test2.c: At top level:
test2.c:102: parse error before `*'
test2.c:102: parse error before `*'
test2.c: In function `RemoveItem':
test2.c:104: `listelement' undeclared (first use in this function)
test2.c:104: `tempp' undeclared (first use in this function)
test2.c:105: `listpointer' undeclared (first use in this function)
test2.c: At top level:
test2.c:111: parse error before `*'
test2.c: In function `PrintQueue':
test2.c:113: `listpointer' undeclared (first use in this function)
test2.c: At top level:
test2.c:123: parse error before `*'
test2.c: In function `ClearQueue':
test2.c:125: `listpointer' undeclared (first use in this function)
0
Comment
Question by:zaaki
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 120 total points
ID: 12526024
instead of
typedef struct listelement{
    int     dataitem;
    struct listelement *link;
};


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


lets see how many errors go
0
 

Author Comment

by:zaaki
ID: 12529324
Waw ! ! !

The errors were reduced to the following:

coffee> gcc test2.c -o test2
test2.c: In function `AddItem':
test2.c:83: parse error before `*'
test2.c:85: `new_lp' undeclared (first use in this function)
test2.c:85: (Each undeclared identifier is reported only once
test2.c:85: for each function it appears in.)

PLEASE help me . . .
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12530472
Sorry for the delayed response.

reframe your Additem() function
as follows and lets know if it still gives you
errors.

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

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

hope this helps,
van_dy
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

850 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