Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

syntax error in program

syntax syntax error

  • Please log in to reply
5 replies to this topic

#1 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 03:54 AM

I am writing a small program
#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int index=0;
typedef struct node tree;
void create_tree(int);
int main()
{
	int i, j, value;
	printf("enter value \n");
	scanf("%d", &value);
	create_tree(value);
}

void create_tree(int num)
{
	tree *temp;
	if (index == 0) {
		root = (tree *) malloc(sizeof(tree));
		root->data = num;
	}
	if(index>0){
	temp=(tree *) malloc(sizeof(tree));
        tree->data=num;

	}
	index++;
}

but I got following syntax error
tree.c:7: warning: built-in function ‘index’ declared as non-function
tree.c: In function ‘create_tree’:
tree.c:27: error: expected identifier or ‘(’ before ‘->’ token
What are these errors and why am I getting the above warnings?
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 September 2010 - 04:37 AM

For the last, tree->data should be temp->data

One of your includes brought in a function called index() that is hidden by your variable index.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 04:44 AM

Oh can you point me which of
stdlib.h or stdio.h has that.
  • 0

#4 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 04:56 AM

Also I modified the code and now it gives a new warning
#include<stdio.h>
#include<stdlib.h>
struct node {
	struct node *left, *right;
	int data, color;
} *root;
int check=0;
typedef struct node tree;
tree * create_tree(int);
int main()
{
	int i, j, value;
	tree *nv;
	printf("enter value \n");
	scanf("%d", &value);
	nv=create_node(value);
}

tree * create_node(int num)
{
	tree *temp;
	if (check == 0) {
		root = (tree *) malloc(sizeof(tree));
		root->data = num;
	}
	if(check!=0){
	
	temp=(tree *) malloc(sizeof(tree));
        temp->data=num;

	}
	check++;
       return temp;
}
the warning is
tree.c: In function ‘main’:
tree.c:16: warning: assignment makes pointer from integer without a cast
tree.c: At top level:
tree.c:19: error: conflicting types for ‘create_node’
tree.c:16: note: previous implicit declaration of ‘create_node’ was here
Why is this warning/error present and how can I overcome it?
I want to have tree as typedef to a pointer to a structure of type node.
  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 September 2010 - 08:02 AM

Where is your other declaration of create_node?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 onus

onus

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 113 posts

Posted 26 September 2010 - 08:16 AM

Ok thanks.
  • 0





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download