473,386 Members | 1,674 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,386 software developers and data experts.

a simple struct code failing

#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;
}
Sep 12 '08 #1
12 6450

"saurabh" <sa*****@nowhere.nodomainwrote in message
news:ga**********@aioe.org...
#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.

--
Bartc

Sep 12 '08 #2
On Sep 12, 12:12 pm, saurabh <saur...@nowhere.nodomainwrote:
#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]
Sep 12 '08 #3
saurabh <sa*****@nowhere.nodomainwrites:
[...]
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.))

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #4
On Fri, 12 Sep 2008 19:12:36 +0200, saurabh wrote:
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
Sep 12 '08 #5
viza <to******@gm-il.com.obviouschange.invalidwrites:
On Fri, 12 Sep 2008 19:12:36 +0200, saurabh wrote:
[...]
> 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;
[...]
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''.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #6

"viza" <to******@gm-il.com.obviouschange.invalidwrote in message
news:dY*******************@newsfe10.ams2...
On Fri, 12 Sep 2008 19:12:36 +0200, saurabh wrote:
>Hi gurus,

Don't assume that anyone who posts here is an expert!
You seem to have not declared nodeptr, but that is not the error you are
asking about:
You mean, the 'nodeptr' declared in this line:
>node * make_n(node * nodeptr,int n)

--
Bartc

Sep 12 '08 #7
Keith Thompson wrote:
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

Sep 12 '08 #8
On Fri, 12 Sep 2008 19:45:10 +0000, Bartc wrote:
"viza" <to******@gm-il.com.obviouschange.invalidwrote
>On Fri, 12 Sep 2008 19:12:36 +0200, saurabh wrote:
>>Hi gurus,

Don't assume that anyone who posts here is an expert!
>You seem to have not declared nodeptr, but that is not the error you
are asking about:

You mean, the 'nodeptr' declared in this line:
>>node * make_n(node * nodeptr,int n)
Sigh. All in all a worthwhile post there then...
Sep 12 '08 #9
Ralf Damaschke <rw****@gmx.dewrites:
Keith Thompson wrote:
>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.
D'oh!

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

But hey, I *could* have preceded it with ``#define int (int)''. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #10
Bartc wrote:
"saurabh" <sa*****@nowhere.nodomainwrote in message
>#include<stdio.h>
#include<stdlib.h>

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.
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 12 '08 #11
viza wrote:
saurabh wrote:
>I am trying to compile the following code ,And I am getting an
> 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:
Yes he has declared it. Look in the parameter list. Parameters
are basically initialized local variables.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 12 '08 #12
On Fri, 12 Sep 2008 19:49:43 -0400, CBFalconer wrote:
viza wrote:
>saurabh wrote:
>>I am trying to compile the following code ,And I am getting an
>> 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:

Yes he has declared it. Look in the parameter list. Parameters are
basically initialized local variables.
Thanks a lot guys,I understood this.
Sep 14 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Curious | last post by:
Hi List, Obviously my google-fu is very weak and I can't find a simple sample code for C++ that allows me to simply download the source of a specified web page. I'm looking at downloading the...
22
by: Tommy | last post by:
Hi all. I am studying computer security, and I got this short and simple (?) c-code. Something is logical wrong in this code, and if used in the wrong hands of someone, it could be taken advantage...
3
by: candy | last post by:
why this code enters an infinite loop. i dont know what is the problem in while((c=fgetc(file))!='\n' && c!='\r') It enters the infinte loop in this statement. The aim of the program is to...
7
by: Fernando Cacciola | last post by:
Hi, I'm terribly new at C# (come from C++ land). I'm making some benchmarks to see the effect of different coding styles, and I run across a situation that strikes me as pretty odd. For my image...
1
by: shenhaipeng | last post by:
Guys: An ACCESS-newbie is knocking the door ... I am wondering whether it is possible to run a simple SQL code to add an autonumber field from within a Make-Table query. Right now I am...
10
by: Potatoman | last post by:
i have some an array of structs, i need to compare arrays. what can you do me for ?thanx Potatoman
6
by: sklett | last post by:
In the past I've had problems with using threads and getting all kinds of weird bugs. I've been reading over Jon Skeet's GREAT website about threading and have tried to apply some of what I...
1
by: disha manikumar | last post by:
Simple java code to read an excel sheet and extract only few fields by treating the excel sheet as a flat file. Can anyone suggest a simple java code for this?
2
by: Cromulent | last post by:
Okay here is the question, what is wrong with this? typedef struct nntpServerInfo { char login; char password; char serverName; int port; } nntpServerInfo;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.