getting syntax error in code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahuls
    New Member
    • Mar 2007
    • 9

    getting syntax error in code

    Hi everyone, I have a couple of questions?
    1.this is part of my main function
    Code:
    //////////////////////////////
    int main()
    { 
       FILE *f1;
    
       int i,j;
       j=100000000;
       int intData[1000];
       f1=fopen("out3.txt","wt"); 
    
       /* Generating 1000 integers from 100,000,001 to 100,001,000 */
       for (i =0; i <1000; i++)
       {
          intData[i] = ++j;
       } 
    /////////////////////////////
    when i try to compile it i get syntax error before j as well as f1?
    Can anyone provide clues on why is that happening?

    2.
    Code:
    ////////////////////////// 
    struct t_node
    {
       void *item;
       struct t_node *left;
       struct t_node *right;
    } node;
    //////////////////////////
    i get an warning : structure defined inside inside parms on compiling
    Code:
    //////////////////////////////
    struct t_collection
    {
       /* Note that size is not needed any longer! */
       int (*ItemCmp)( void *, void * ); 
       struct t_node *node;
    };
    ////////////////////////////////////////
    even this one gives 2 warning saying that
    warning : structure defined inside parms
    warning: empty declarations

    Any help is appreciated. Thanks in advance.
    Last edited by Ganon11; Mar 21 '07, 12:26 AM. Reason: code tags added
  • Roonie
    New Member
    • Mar 2007
    • 99

    #2
    need you a comma between i and j as you declare them?

    i also believe that open() is a function . . . use
    Code:
    f1.open(<params>);
    try that, then recompile.

    Comment

    • Roonie
      New Member
      • Mar 2007
      • 99

      #3
      well, i guess the comma shows up now with the code tags . . .

      youre trying to store a value in j that is too big for an int. (may or may not be the problem) use a long int.

      Comment

      • Roonie
        New Member
        • Mar 2007
        • 99

        #4
        also, when you embed a t_node into your other structs, you need not use the keyword struct again . . . it tells the compiler you are defining the struct again.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by Roonie
          also, when you embed a t_node into your other structs, you need not use the keyword struct again . . . it tells the compiler you are defining the struct again.
          This may or may not be true...I think in C you always have to use the keyword struct in front of a variable declaration (for types of that struct) - in other words, he has to use struct t_node *left;. If this is in C++ then I agree with you; if not I'm not so sure.

          Comment

          • rahuls
            New Member
            • Mar 2007
            • 9

            #6
            Originally posted by Ganon11
            This may or may not be true...I think in C you always have to use the keyword struct in front of a variable declaration (for types of that struct) - in other words, he has to use struct t_node *left;. If this is in C++ then I agree with you; if not I'm not so sure.
            Thanks for replying. My program is in C. Those warning show up only when I am compiling in linux platform, not in cygwin for some reason.
            changing int to long int did not help
            basically it says a syntax error before this line
            j=10000000;
            even f1.open does not cause any diff. it is still showing the same errors.
            thanks

            Comment

            • Roonie
              New Member
              • Mar 2007
              • 99

              #7
              Originally posted by rahuls
              My program is in C.
              sorry, i was simply assuming.

              i know far less about c than i do about c++, but i compiled almost exactly what you posted:

              Code:
              #include <stdio.h>
              
              int main()
              { 
                 FILE *f1;
              
                 int i, j;
                 j=100000000;
                 int intData[1000];
                 f1=fopen("out3.txt","wt"); 
              
                 /* Generating 1000 integers from 100,000,001 to 100,001,000 */
                 for (i =0; i <1000; i++)
                 {
                    intData[i] = ++j;
              	  fprintf(f1,"%d",intData[i]); //added this
                 } 
              }
              
              struct t_node
              {
                 void *item;
                 struct t_node *left;
                 struct t_node *right;
              } node;
              
              struct t_collection
              {
                 /* Note that size is not needed any longer! */
                 int (*ItemCmp)( void *, void * ); 
                 struct t_node *node;
              };
              and i got an output file with the appropriate numbers all over it. funny thing is that im pretty sure my compiler thought i was writing in c++ . . . which, needless to say, is odd . . .

              Comment

              • rahuls
                New Member
                • Mar 2007
                • 9

                #8
                so is my code correct, should i try to compile it with g++ then?

                Comment

                • Roonie
                  New Member
                  • Mar 2007
                  • 99

                  #9
                  . . . might be worth a shot. is there any particular reason youre using c?

                  Comment

                  • rahuls
                    New Member
                    • Mar 2007
                    • 9

                    #10
                    Originally posted by Roonie
                    . . . might be worth a shot. is there any particular reason youre using c?
                    ya i am using c because i have been asked to write it in c

                    Comment

                    • Roonie
                      New Member
                      • Mar 2007
                      • 99

                      #11
                      well, ive been fooling around with your code. i got my compiler thinking in c and found the errors you reported . . . and for some reason, initializing j on the same line you declare it seemed to fix it:

                      Code:
                      #include <stdio.h>
                      
                      int main()
                      { 
                         FILE *f1;
                      
                         int i, j = 100000000;
                         int intData[1000];
                         f1=fopen("out3.txt","wt"); 
                      
                         /* Generating 1000 integers from 100,000,001 to 100,001,000 */
                         for (i =0; i <1000; i++)
                         {
                            intData[i] = ++j;
                      	  fprintf(f1,"%d",intData[i]); //added this
                         } 
                      }
                      
                      struct t_node
                      {
                         void *item;
                         struct t_node *left;
                         struct t_node *right;
                      } node;
                      
                      struct t_collection
                      {
                         /* Note that size is not needed any longer! */
                         int (*ItemCmp)( void *, void * ); 
                         struct t_node *node;
                      };
                      which seems really wierd to me since, theoretically, both ways should be valid. (if anyone else knows something here, please jump right in . . .)

                      Comment

                      • rahuls
                        New Member
                        • Mar 2007
                        • 9

                        #12
                        well dude its still not working for me.
                        I think its time i tell you more about my code and the compiling errors which I am getting. I have been asked to implement a binary tree structure with the foll requirements
                        1) Generate 1000 integers from 100,000,001 to 100,001,000
                        2) Create a collection and add first 500 integers
                        3) Delete 400 integers from 101st to 500th.
                        4) Add the rest of 500 integers
                        5) Find all the odd numbers in the collection and delete all of them.
                        6) Your inordertraversa l function writes all the items in the collection into a file named output3.txt as below.
                        100000002
                        100000004
                        100000006
                        ....
                        i have written the code with 3 files which are as follows:
                        ////////////////////////////////////////
                        main.c
                        /////////////////////////////////
                        #include <assert.h>
                        #include <string.h>
                        #include <math.h>
                        #include "coll_a.h" /* import the specification */

                        collection myCollection;
                        int max_items;

                        int private_print(v oid *item);
                        int ItemCmp(void *a, void *b);

                        int main()
                        {
                        FILE *f1;
                        int i,j=100000000;
                        int intData[1000];
                        f1=fopen("out3. txt","wt");

                        /* Generating 1000 integers from 100,000,001 to 100,001,000 */
                        for (i =0; i <1000; i++)
                        {
                        intData[i] = ++j;
                        }
                        max_items = 1000;
                        j = 0;
                        myCollection = ConsCollection( max_items,ItemC mp);
                        printf("adding 500 items\n");
                        for (i = 0; i <500; i++)
                        {
                        AddToCollection (myCollection, (void *)&intData[j ++]);
                        }
                        printf("deletin g 400 items\n");
                        for (i = 0; i <400; i++)
                        {
                        DeleteFromColle ction(myCollect ion, (void *)&intData[--j]);
                        }
                        printf("adding the rest of 500 items\n");
                        j+=400;
                        for (i = 0; i <500; i++)
                        {
                        AddToCollection (myCollection, (void *)&intData[++j]);
                        }
                        for (i=0;i <100;i++)
                        {
                        if((intData[i]%2)!=0)
                        {DeleteFromColl ection(myCollec tion, (void *)&intData[i]);
                        }
                        for (i=500;i<1000;i ++)
                        {
                        if((intData[i]%2)!=0)
                        {DeleteFromColl ection(myCollec tion, (void *)&intData[i]);
                        }
                        }
                        InOrderTraversa l(c,private_pri nt);
                        }

                        int ItemCmp(void *a, void *b)
                        {long *d1,*d2;
                        *d1=(long *)a;
                        *d2=(long *)b;
                        if(*d1>*d2)
                        return -1;
                        else if(*d1<*d2)
                        return 1;
                        else
                        return 0;
                        }
                        int private_print(v oid *item)
                        {long *t;
                        *t=(long *)item;
                        if(t!=NULL)
                        {printf("%d",*t );
                        fprintf (f1, "\n%d",*t);
                        }
                        else
                        {
                        return 0;
                        }
                        }
                        ///////////////////////////////////////////
                        /////////////////////////////////////////////////////////
                        coll_a.h
                        ///////////////////////////////////////
                        /* Specification for collection */

                        typedef struct t_collection *collection;

                        collection ConsCollection( int max_items,
                        int (*ItemCmp)(void *, void *) );
                        /* Construct a new collection
                        Pre-condition: max_items > 0
                        Post-condition: returns a pointer to an empty collection
                        */

                        void AddToCollection ( collection c, void *item );
                        /* Add an item to a collection
                        Pre-condition: (c is a collection created by a call to
                        ConsCollection) &&
                        (existing item count < max_items) &&
                        (item != NULL)
                        Post-condition: item has been added to c
                        */

                        void DeleteFromColle ction( collection c, void *item );
                        /* Delete an item from a collection
                        Pre-condition: (c is a collection created by a call to
                        ConsCollection) &&
                        (existing item count >= 1) &&
                        (item != NULL)
                        Post-condition: item has been deleted from c
                        */

                        void *FindInCollecti on( collection c, void *key );
                        /* Find an item in a collection
                        Pre-condition: c is a collection created by a call to
                        ConsCollection
                        key != NULL
                        Post-condition: returns an item identified by key if one
                        exists, otherwise returns NULL
                        */

                        void InOrderTraversa l(collection c, int(*private_pr int)(void *item))

                        //////////////////////////////////////////////////////////////
                        Here are the errors which I get on compiling.

                        ont> gcc -c main.c coll_at.c
                        main.c: In function `InOrderTravers al':
                        main.c:17: error: syntax error before '{' token
                        main.c:20: error: parameter `j' is initialized
                        main.c:22: error: syntax error before "f1"
                        coll_at.c: In function `InOrderTravers al':
                        coll_at.c:13: warning: structure defined inside parms
                        coll_at.c:20: warning: structure defined inside parms
                        coll_at.c:20: warning: empty declaration
                        coll_at.c:23: error: syntax error before ')' token
                        coll_at.c:92: error: syntax error before '*' token

                        Plz help because I need to submit this by tom. Thanks a lot

                        Comment

                        • rahuls
                          New Member
                          • Mar 2007
                          • 9

                          #13
                          the file coll_at.c
                          //////////////////
                          coll_at.c
                          ///////////////////////////////////////
                          #include <stdlib.h> /* calloc */
                          #include <stdio.h> /* NULL */
                          #include <assert.h> /* Needed for assertions */
                          #include "coll_a.h" /* import the specification */

                          struct t_node
                          {
                          void *item;
                          struct t_node *left;
                          struct t_node *right;
                          } node;

                          struct t_collection
                          {
                          /* Note that size is not needed any longer! */
                          int (*ItemCmp)( void *, void * );
                          struct t_node *node;
                          };

                          collection ConsCollection( int max_items,
                          int (*ItemCmp)(void *,void *))
                          /* Construct a data collection
                          Pre-condition: (max_items > 0) && (item_size > 0)
                          Post-condition: returns a pointer to an empty
                          collection
                          */
                          {
                          collection c;
                          /* Although redundant, this assertion should be
                          retained as it tests compliance with the formal
                          specification */
                          assert( max_items > 0 );
                          c = (collection)cal loc( 1, sizeof(struct t_collection) );
                          c->node = (struct t_node *)0;
                          c->ItemCmp = ItemCmp;
                          return c;
                          }

                          static void AddToTree( struct t_node **t, struct t_node *data,
                          int (*ItemCmp)(void *, void *) )
                          {
                          struct t_node *base;
                          base = *t;
                          /* If it's a null tree, just add it here */
                          if ( base == NULL )
                          {
                          *t = data;
                          return;
                          }
                          else
                          {
                          if ( ItemCmp( base->item, data ) < 0 )
                          {
                          AddToTree( &(base->left), data, ItemCmp );
                          }
                          else
                          AddToTree( &(base->right), data, ItemCmp );
                          }
                          }

                          void AddToCollection ( collection c, void *item )
                          /* Add an item to a collection
                          Pre-condition: (c is a collection created by a call to
                          ConsCollection) &&
                          (existing item count < max_items) &&
                          (item != NULL)
                          Post-condition: item has been added to c
                          */
                          {
                          struct t_node *data, *node_p;
                          assert( c != NULL );
                          assert( item != NULL );
                          /* Allocate space for a node for the data item */
                          data = (struct t_node *)malloc(sizeof (struct t_node));
                          /* Attach the item to the node */
                          data->item = item;
                          data->left = data->right = (struct t_node *)0;
                          node_p = c->node;
                          AddToTree( &node_p, data, c->ItemCmp );
                          c->node=node_p;
                          }



                          void DeleteFromTree( struct t_node **t, void *item )
                          {
                          struct t_node *temp, *prev, *succ;
                          prev=temp=*t;
                          void *val;
                          *val=(void*)ite m;
                          //val=item;
                          if(*t==NULL)
                          printf("NO NODES TO BE DELETED");
                          else
                          {
                          while(temp->item!=val && temp!=NULL) //define val
                          {
                          prev=temp;
                          if(temp->item >val)
                          temp=temp->left;
                          else
                          temp=temp->right;
                          }
                          if(temp==NULL)
                          {
                          printf("the value could not be found\n");
                          return;
                          }
                          if(temp->left!=NULL && temp->right!=NULL)
                          {
                          prev=temp;
                          succ=temp->right;
                          while(succ->left!=NULL)
                          {
                          prev=succ;
                          succ=succ->left;
                          }
                          temp->item=succ->item;
                          temp=succ;
                          }
                          if(temp->left!=NULL && temp->right==NULL)
                          {
                          if(prev->left==temp)
                          prev->left=temp->left;
                          else
                          prev->right=temp->left;
                          }
                          if(temp->left==NULL && temp->right!=NULL)
                          {
                          if(prev->left==temp)
                          prev->left=temp->right;
                          else
                          prev->right=temp->right;
                          }
                          if(temp->left==NULL && temp->right==NULL)
                          {
                          if(prev->left==temp)
                          prev->left=NULL;
                          else
                          prev->right=NULL;
                          }
                          /* if(temp==*root) // define root
                          {
                          if(temp->left!=NULL)
                          *root=temp->left;
                          else
                          *root=temp->right;
                          }*/
                          free(temp);
                          }


                          void DeleteFromColle ction( collection c, void *item )
                          /* Delete an item from a collection
                          Pre-condition: (c is a collection created by a call to
                          ConsCollection) &&
                          (existing item count >= 1) &&
                          (item != NULL)
                          Post-condition: item has been deleted from c
                          */
                          {
                          struct t_node *node;
                          assert( c != NULL );
                          /* The requirement that the collection has at least
                          one item is expressed a little differently */
                          assert( c->node != NULL );
                          assert( item != NULL);
                          /* Select node at head of list */
                          node = c->node;
                          DeleteFromTree( &node, item );
                          c->node=node;
                          }

                          void *FindInTree( struct t_node *t, void *key )
                          {
                          if(t==NULL)
                          return;
                          else if(t->item==key)
                          return t;
                          else if ( ItemCmp( t->item, key ) < 0 )
                          return FindInTree( (t->left), key );
                          else
                          return FindInTree( (t->right), key );
                          }





                          void *FindInCollecti on( collection c, void *key )
                          /* Find an item in a collection
                          Pre-condition: (c is a collection created by a call to
                          ConsCollection) &&
                          (key != NULL)
                          Post-condition: returns an item identified by key if
                          one exists, otherwise returns NULL
                          */
                          {
                          struct t_node *node;
                          assert( c != NULL );
                          assert( key != NULL );
                          /* Select node at head of list */
                          node = c->node;
                          return FindInTree( node, key );
                          }
                          }



                          void InOrderTreeTrav ersal(struct t_node *t, int(*private_pr int)(void *item))
                          {
                          if(t != NULL)
                          {
                          InOrderTreeTrav ersal(t->left,private_p rint);
                          private_print(t->item);
                          InOrderTreeTrav ersal(t->right, private_print);
                          }
                          }

                          void InOrderTraversa l(collection c, int(*private_pr int)(void *item))
                          {
                          assert(c != NULL);
                          InOrderTreeTrav ersal(c->node,private_p rint);
                          }

                          Comment

                          • DeMan
                            Top Contributor
                            • Nov 2006
                            • 1799

                            #14
                            One of the problems is in :

                            void InOrderTraversa l(collection c, int(*private_pr int)(void *item))

                            int(*private_pr int)(void *item)) is not right,
                            It is not entirely clear what the intentiobn is here, but I suspect it's one of 3 things:
                            there should proably be a comma bwetween items,

                            I don';t think you can play games with casting in a method declaration (and if you can the casting you are doing needs some rearranging of parenthesis)

                            If you are trying to pass a method in, try declaring a pointer to the method somewhere.....
                            Have a look here for more information

                            Comment

                            • DeMan
                              Top Contributor
                              • Nov 2006
                              • 1799

                              #15
                              Here are some ideas on the other issues (which may niot fix the problem, but we'll see)
                              main.c:17: error: syntax error before '{' token
                              try declaring main to take arguments:
                              Code:
                              int main (int args, char argv[]);
                              main.c:20: error: parameter `j' is initialized
                              try either declaring i and j on seperate lines, or declare both here but set j later
                              Code:
                              int i;
                              int j = 100,000,000;
                               OR
                              int i, j;
                              
                              /* code in between */
                              
                              /* before for loop */
                              j = 100,000,000;
                              main.c:22: error: syntax error before "f1"
                              Fixing the other two might get rid of this alkso

                              Comment

                              Working...