By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

getting syntax error in code

P: 9
Hi everyone, I have a couple of questions?
1.this is part of my main function
Expand|Select|Wrap|Line Numbers
  1. //////////////////////////////
  2. int main()
  3.    FILE *f1;
  4.  
  5.    int i,j;
  6.    j=100000000;
  7.    int intData[1000];
  8.    f1=fopen("out3.txt","wt"); 
  9.  
  10.    /* Generating 1000 integers from 100,000,001 to 100,001,000 */
  11.    for (i =0; i <1000; i++)
  12.    {
  13.       intData[i] = ++j;
  14.    } 
  15. /////////////////////////////
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.
Expand|Select|Wrap|Line Numbers
  1. ////////////////////////// 
  2. struct t_node
  3. {
  4.    void *item;
  5.    struct t_node *left;
  6.    struct t_node *right;
  7. } node;
  8. //////////////////////////
i get an warning : structure defined inside inside parms on compiling
Expand|Select|Wrap|Line Numbers
  1. //////////////////////////////
  2. struct t_collection
  3. {
  4.    /* Note that size is not needed any longer! */
  5.    int (*ItemCmp)( void *, void * ); 
  6.    struct t_node *node;
  7. };
  8. ////////////////////////////////////////
even this one gives 2 warning saying that
warning : structure defined inside parms
warning: empty declarations

Any help is appreciated. Thanks in advance.
Mar 21 '07 #1
Share this Question
Share on Google+
18 Replies


Roonie
P: 99
need you a comma between i and j as you declare them?

i also believe that open() is a function . . . use
Expand|Select|Wrap|Line Numbers
  1. f1.open(<params>);
  2.  
try that, then recompile.
Mar 21 '07 #2

Roonie
P: 99
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.
Mar 21 '07 #3

Roonie
P: 99
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.
Mar 21 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
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.
Mar 21 '07 #5

P: 9
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
Mar 21 '07 #6

Roonie
P: 99
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4.    FILE *f1;
  5.  
  6.    int i, j;
  7.    j=100000000;
  8.    int intData[1000];
  9.    f1=fopen("out3.txt","wt"); 
  10.  
  11.    /* Generating 1000 integers from 100,000,001 to 100,001,000 */
  12.    for (i =0; i <1000; i++)
  13.    {
  14.       intData[i] = ++j;
  15.       fprintf(f1,"%d",intData[i]); //added this
  16.    } 
  17. }
  18.  
  19. struct t_node
  20. {
  21.    void *item;
  22.    struct t_node *left;
  23.    struct t_node *right;
  24. } node;
  25.  
  26. struct t_collection
  27. {
  28.    /* Note that size is not needed any longer! */
  29.    int (*ItemCmp)( void *, void * ); 
  30.    struct t_node *node;
  31. };
  32.  
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 . . .
Mar 21 '07 #7

P: 9
so is my code correct, should i try to compile it with g++ then?
Mar 21 '07 #8

Roonie
P: 99
. . . might be worth a shot. is there any particular reason youre using c?
Mar 21 '07 #9

P: 9
. . . 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
Mar 21 '07 #10

Roonie
P: 99
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4.    FILE *f1;
  5.  
  6.    int i, j = 100000000;
  7.    int intData[1000];
  8.    f1=fopen("out3.txt","wt"); 
  9.  
  10.    /* Generating 1000 integers from 100,000,001 to 100,001,000 */
  11.    for (i =0; i <1000; i++)
  12.    {
  13.       intData[i] = ++j;
  14.       fprintf(f1,"%d",intData[i]); //added this
  15.    } 
  16. }
  17.  
  18. struct t_node
  19. {
  20.    void *item;
  21.    struct t_node *left;
  22.    struct t_node *right;
  23. } node;
  24.  
  25. struct t_collection
  26. {
  27.    /* Note that size is not needed any longer! */
  28.    int (*ItemCmp)( void *, void * ); 
  29.    struct t_node *node;
  30. };
which seems really wierd to me since, theoretically, both ways should be valid. (if anyone else knows something here, please jump right in . . .)
Mar 21 '07 #11

P: 9
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 inordertraversal 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(void *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,ItemCmp);
printf("adding 500 items\n");
for (i = 0; i <500; i++)
{
AddToCollection(myCollection, (void *)&intData[j ++]);
}
printf("deleting 400 items\n");
for (i = 0; i <400; i++)
{
DeleteFromCollection(myCollection, (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)
{DeleteFromCollection(myCollection, (void *)&intData[i]);
}
for (i=500;i<1000;i++)
{
if((intData[i]%2)!=0)
{DeleteFromCollection(myCollection, (void *)&intData[i]);
}
}
InOrderTraversal(c,private_print);
}

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(void *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 DeleteFromCollection( 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 *FindInCollection( 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 InOrderTraversal(collection c, int(*private_print)(void *item))

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

ont> gcc -c main.c coll_at.c
main.c: In function `InOrderTraversal':
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 `InOrderTraversal':
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
Mar 21 '07 #12

P: 9
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)calloc( 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*)item;
//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 DeleteFromCollection( 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 *FindInCollection( 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 InOrderTreeTraversal(struct t_node *t, int(*private_print)(void *item))
{
if(t != NULL)
{
InOrderTreeTraversal(t->left,private_print);
private_print(t->item);
InOrderTreeTraversal(t->right, private_print);
}
}

void InOrderTraversal(collection c, int(*private_print)(void *item))
{
assert(c != NULL);
InOrderTreeTraversal(c->node,private_print);
}
Mar 21 '07 #13

DeMan
100+
P: 1,806
One of the problems is in :

void InOrderTraversal(collection c, int(*private_print)(void *item))

int(*private_print)(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
Mar 21 '07 #14

DeMan
100+
P: 1,806
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:
Expand|Select|Wrap|Line Numbers
  1. int main (int args, char argv[]);
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int j = 100,000,000;
  3.  OR
  4. int i, j;
  5.  
  6. /* code in between */
  7.  
  8. /* before for loop */
  9. j = 100,000,000;
  10.  
main.c:22: error: syntax error before "f1"
Fixing the other two might get rid of this alkso
Mar 21 '07 #15

Ganon11
Expert 2.5K+
P: 3,652
Of course, you should declare j as

Expand|Select|Wrap|Line Numbers
  1. j = 100000000;
rather than 100,000,000 since the commas will confuse the computer.
Mar 21 '07 #16

P: 9
well after making the changes for main and declaration of j, i got some new compile errors.
The first error says that main.c In function 'InOrderTraversal'. Now I really dont understand how this function came into main.
Also, I have been asked in the question to define the prototype function as
The prototype function is

void InOrderTraversal (collection c, int (*private_print)(void *item))
So, what is exactly wrong with this?



gcc -c main.c coll_at.c
main.c: In function `InOrderTraversal':
main.c:15: error: old-style parameter declarations in prototyped function definition
main.c:37: error: `ItemCmp' undeclared (first use in this function)
main.c:37: error: (Each undeclared identifier is reported only once
main.c:37: error: for each function it appears in.)
main.c: In function `ItemCmp':
main.c:78: warning: assignment makes integer from pointer without a cast
main.c:79: warning: assignment makes integer from pointer without a cast
main.c: In function `InOrderTraversal':
main.c:89: error: 'private_print' redeclared as different kind of symbol
coll_a.h:42: error: previous definition of 'private_print' was here
main.c: In function `private_print':
main.c:90: warning: assignment makes integer from pointer without a cast
main.c: In function `InOrderTraversal':
main.c:99: error: syntax error at end of input
coll_at.c: In function `InOrderTraversal':
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:29: error: syntax error before '{' token
coll_at.c:92: error: syntax error before '*' token
Mar 21 '07 #17

DeMan
100+
P: 1,806
I suspect you should read up on function pointers. Most of these problems seem to be caused by passing pointers to functions around the place. This is not something I have done terribly often, so it is difficult to pinpoint the exact cause of the problem....
The link in my earlier post has a detailed explanation of how to use function pointers.
Mar 22 '07 #18

P: 9
Can any other expert please help me on this?
Mar 22 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.