• C

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

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)
zaakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zaakiAuthor Commented:
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
van_dyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.