please check

R

raghu

I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int num=10;
struct node *p;
p=NULL;
append(&p,num);
getch();
}
struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->link=NULL;
return p;
}
append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
return 0;
}
 
A

Andrew Poelstra

I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
<snipped similar>

Could you format and try again? From what I saw, there's a /lot/ of
problems with your code, but I'd like to be able to read it.

There's no such thing as <alloc.h>.
 
J

jwalker

raghu said:
I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int num=10;
struct node *p;
p=NULL;
append(&p,num);
getch();
}
struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->link=NULL;
return p;
}
append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
return 0;
}

This is my first post to comp.lang.c, so hello to all there.

You call firstnode function with invalid parameter - 'int'. Try to
place real int parameter- variable or constant, may be 'num' that is
parameter of append() function. You now are triyng to send the type int
which is not allowed.
 
R

Richard

Andrew Poelstra said:
<snipped similar>

Could you format and try again? From what I saw, there's a /lot/ of
problems with your code, but I'd like to be able to read it.

There's no such thing as <alloc.h>.

Yes there is. It might not be ISO standard, but there sure as hell are
alloc.h implementations out there.

To the OP :

Check out your call to firstnode in the append fnuction. Why are you
passing "int" as opposed to a number?
 
F

Fred Kleinschmidt

raghu said:
I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int num=10;
struct node *p;
p=NULL;
append(&p,num);
getch();
}
struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->link=NULL;
return p;
}
append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////
-----------------^

Do you mean
*q= firstnode(num);
 
N

Nick Keighley

raghu said:
I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

I fixed your crap layout. My comment are marked "***nik"

#include <stdio.h>
#include <alloc.h>

/***nik non-standard header replace with stdlib.h ***/

struct node
{
int data;
struct node *link;
};

void main()

/***nik int main(void) ***/

{
int num = 10;
struct node *p;
p = NULL;
append(&p,num);

/***nik no prototype in scope. Didn't your compiler tell you this?
I'd put main() at the end ***/

getch();
/***nik why? ***/

}


struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
/***nik don't cast malloc() ***/


p->data = num;
p->link = NULL;
return p;
}


append(struct node **q,int num)
/***nik no return type ***/

{
struct node *temp,*r;

if(*q == NULL)
*q = firstnode(int); ////////////////////////////
/*** *q = firstnode(num); maybe? ***/

else
{
temp = *q;
while (temp->link != NULL)
temp = temp->link;

r = malloc(sizeof(struct node));
r->data =n um;
r->link = NULL;
temp->link = r;
}
}
 
S

santosh

raghu said:
I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

Next time, please use indentation.
#include<stdio.h>
#include<alloc.h>

Use said:
struct node
{
int data;
struct node *link;
};
void main()

In standard C, main() must return integer and it's better to explicitly
indicate lack of parameters with the void keyword. Hence the better
form of the above would be:

int main(void)
{
int num=10;

Using a symbolic constant instead of a literal is more robust.
struct node *p;
p=NULL;
append(&p,num);

Since append() is called before it's defined, you should provide a
prototype for it. Place it after the #include statements.

This is a non-standard function and appears to be pointlessly called
here. Remove it.
}
struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));

In C, you need not cast the return value of malloc(). Doing so may
circumvent compiler diagnostics which may otherwise be useful. Also,
where possible, use the sizeof() operator on the relevant object rather
than it's type. If, later on, you modify the type of the object, you
need not touch the malloc() statement. It's automatically updated to
the correct type.

So a better form of the above statement is:

p = malloc(sizeof *p);

Also check the malloc() call for failure before proceeding.
p->data=num;
p->link=NULL;
return p;
}
append(struct node **q,int num)

Use int before append() to indicate the return value.
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////

I think you meant to write:

*q = firstnode(num);
 
R

raghu

Thanks a lot guys. I shall follow your way of programming.. Thanks once
again for spending your valuable time in answering my problem.
 
C

Christopher Benson-Manica

raghu said:
I have written code for single linked list...I get an error as
Expression syntax in the line marked with ////////. Please correct me
where am I going wrong. I compiled this in TURBO Compiler

I took the liberty of reformatting your code below. You have a number
of major problems, the syntax error you are seeing being only the most
obvious. int is a type, not a number, and it suggests that it's time
to take another look at your friendly C reference. (I leave
discussion of your other issues to other posters.)

(OP's code, reformatted with spaces, follows.)

#include<stdio.h>
#include<alloc.h>

struct node
{
int data;
struct node *link;
};

void main()
{
int num=10;
struct node *p;
p=NULL;
append(&p,num);
getch();
}

struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->link=NULL;
return p;
}

append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
return 0;
}
}
 
K

Keith Thompson

santosh said:
raghu wrote:

In standard C, main() must return integer and it's better to explicitly
indicate lack of parameters with the void keyword. Hence the better
form of the above would be:

int main(void)

Correction: main() must return int.

int is a specific type; the word "integer" refers to the entire set of
integer types (char, short, int, long, etc.). It's a very important
distinction.
 
A

Andrew Poelstra

I took the liberty of reformatting your code below. You have a number
of major problems, the syntax error you are seeing being only the most
obvious. int is a type, not a number, and it suggests that it's time
to take another look at your friendly C reference. (I leave
discussion of your other issues to other posters.)

(OP's code, reformatted with spaces, follows.)

#include<stdio.h>
#include<alloc.h>

struct node
{
int data;
struct node *link;
};

void main()

main() returns int. main() returns int. main() returns int.

int main(void)
{
int num=10;
struct node *p;
p=NULL;

Combine this into
struct node *p = NULL;
append(&p,num);

No prototype. How can I know what this returns and accepts as
parameters?

Get rid of this. It just pisses people off.
}

struct node *firstnode(int num)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));

p = malloc(sizeof *p);

Don't cast the return of malloc(). It hides the fact that you didn't
#include <stdlib.h>, it clutters the code, and it it poor style.

Don't use sizeof(type). It's almost never necessary, and it hinders
maintenance.
p->data=num;
p->link=NULL;
return p;
}

append(struct node **q,int num)

Don't depend on implicit int. It's bad style, and illegal in C99.
{
struct node *temp,*r;
if(*q==NULL)
*q= firstnode(int); ////////////////////////////

1) Error: "int" is a keyword, not a value.
2) Error: "/" found where it doesn't belong. Several times.
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;

This is almost guaranteed to be a logic error. The loop will never end.
What were you trying to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top