a simple struct code failing

S

saurabh

#include<stdio.h>
#include<stdlib.h>

Hi gurus,
I am trying to compile the following code ,And I am getting an error on the
line labeled /*This one*/.
The error says,
"expected expression before 'node'"
Can someone please explain me the reason?
struct node
{
int data;
struct node* next;

};

typedef struct node node;


node * make_n(node * nodeptr,int n)
{
int i=0;
node* newnode=NULL;
/*Please check the following line*/
nodeptr=(node*)malloc(sizeof node);/*This one*/
nodeptr->next=NULL;
nodeptr->data=1;
node * firstnode=nodeptr;
for(i=2;i<n+1;i++)
{
newnode=(node*)malloc(sizeof node);
newnode->data=(2*i);
newnode->next=NULL;
nodeptr->next=newnode;
nodeptr=newnode;

}
return firstnode;
}
 
B

Bartc

saurabh said:
#include<stdio.h>
#include<stdlib.h>

Hi gurus,
I am trying to compile the following code ,And I am getting an error on
the
line labeled /*This one*/.
The error says,
"expected expression before 'node'"
Can someone please explain me the reason?
nodeptr=(node*)malloc(sizeof node);/*This one*/

Change sizeof node to sizeof (node). And the same a few lines further down.
I think sizeof expects parentheses around type arguments.
 
J

John Bode

#include<stdio.h>
#include<stdlib.h>

Hi gurus,
I am trying to compile the following code ,And I am getting an error on the
line labeled /*This one*/.
The error says,
"expected expression before 'node'"
Can someone please explain me the reason?
struct node
{
int data;
struct node* next;

};

typedef struct node node;

node * make_n(node * nodeptr,int n)
{
int i=0;
node* newnode=NULL;
/*Please check the following line*/
nodeptr=(node*)malloc(sizeof node);/*This one*/

When using the sizeof operator on a type name, the type name must be
enclosed in parentheses:

nodeptr = malloc(sizeof (node));

Personally, I prefer calling sizeof on the object being allocated
(note that the name of the object is *not* in parentheses):

nodeptr = malloc(sizeof *nodeptr);

It's a bit more straightforward to me.

[snip rest]
 
K

Keith Thompson

saurabh said:
I am trying to compile the following code ,And I am getting an error on the
line labeled /*This one*/.
The error says,
"expected expression before 'node'"
Can someone please explain me the reason?
struct node
{
int data;
struct node* next;

};

typedef struct node node; [...]
nodeptr=(node*)malloc(sizeof node);/*This one*/
[...]

The sizeof operator has two forms: "sizeof expr" and "sizeof (type)".
Since node is a type name, "sizeof node" needs to be "sizeof (node)".

But there's a better way to write that entire line:

nodeptr = malloc(sizeof *nodeptr);

Casting the result of malloc is unnecessary, and can mask errors in
some cases. And by using "sizeof *nodeptr", the line remains correct
even if nodeptr is later changed to some other type. (Don't worry
about dereferencing an uninitialized pointer; the argument of sizeof
isn't evaluated. (There are exceptions for variable-length arrays;
that needn't concern you here.))
 
V

viza

Hi gurus,

Don't assume that anyone who posts here is an expert!
I am trying to compile the following code ,And I am getting an error on
node* newnode=NULL;
nodeptr=(node*)malloc(sizeof node);

You seem to have not declared nodeptr, but that is not the error you are
asking about:

sizeof works in two ways:

sizeof(type)
sizeof object

eg:
sizeof(int)

or:
int foo;
sizeof foo;

You can include extra parentheses in the second type, but you cannot omit
them in the first type.

HTH
viza
 
K

Keith Thompson

viza said:
You seem to have not declared nodeptr, but that is not the error you are
asking about:

sizeof works in two ways:

sizeof(type)
sizeof object

eg:
sizeof(int)

or:
int foo;
sizeof foo;
[...]


Correction: it's
sizeof(type)
sizeof expression
(Actually in the second case the expression has to be in a particular
subset of expressions called a "unary-expression".)

Of course the expression can be the name of an object, but it doesn't
have to be; ``sizeof 42'' is equivalent to ``sizeof int''.
 
R

Ralf Damaschke

Keith said:
Correction: it's
sizeof(type)
sizeof expression
(Actually in the second case the expression has to be in a particular
subset of expressions called a "unary-expression".)

Of course the expression can be the name of an object, but it doesn't
have to be; ``sizeof 42'' is equivalent to ``sizeof int''.

Aehem, they differ in that the first expression is legal, the second not.

-- Ralf
 
K

Keith Thompson

Ralf Damaschke said:
Aehem, they differ in that the first expression is legal, the second not.

D'oh!

``sizeof 42'' is equivalent to ``sizeof(int)''.

But hey, I *could* have preceded it with ``#define int (int)''. :cool:}
 
C

CBFalconer

Bartc said:
Change sizeof node to sizeof (node). And the same a few lines further down.
I think sizeof expects parentheses around type arguments.

And after that fix your code. Basically you are confusing the use
of nodeptr and newnode. Also don't cast the returned value from
malloc.
 
C

CBFalconer

viza said:
You seem to have not declared nodeptr, but that is not the error
you are asking about:

Yes he has declared it. Look in the parameter list. Parameters
are basically initialized local variables.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top