473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

getting syntax error in code

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
18 3752
Roonie
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
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
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
3,652 Expert 2GB
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
rahuls
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
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
rahuls
9
so is my code correct, should i try to compile it with g++ then?
Mar 21 '07 #8
Roonie
99
. . . might be worth a shot. is there any particular reason youre using c?
Mar 21 '07 #9
rahuls
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
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
rahuls
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
rahuls
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
1,806 1GB
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
1,806 1GB
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
3,652 Expert 2GB
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
rahuls
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
1,806 1GB
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
rahuls
9
Can any other expert please help me on this?
Mar 22 '07 #19

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

Similar topics

15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
9
by: Brian | last post by:
So I have this: double x = .25; double y = .5; double z = .25; double probability; int index; while(index < 30) {
2
by: coolindienc | last post by:
Today I am going nuts. This is another one that is not working. It keeps giving me syntax error at highlighted line. No matter what I do I still have that error at the same line. It seems that my...
4
by: aalmakto | last post by:
Hi all, Whats wrong with this syntax? DELETE from fighterSponsor fs, sponsors s, imageData id where fs.sponsorId=s.sponsorId AND id.imageId=s.imageId AND (s.imageId=81 OR s.imageId=82) I...
8
by: illuzion | last post by:
ok I keep getting this error: Parse error: syntax error, unexpected T_VARIABLE in /home/illuzion/public_html/BAMF/contactus.php on line 38 and this error is possibly on other lines could...
1
by: kigoobe | last post by:
Hi friends, I'm having three queries that works perfectly ... SELECT ib.id as id, ib.titre as title, ib.date_expire as date_fin, ib.created_at as date_creation, eb.content as content, '' FROM...
1
by: cwfontan | last post by:
I am uploading a file renaming it with text from input here is the sql insert code and error im getting. If you need more of the code let me know.. This exact code is working when inserting into...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.