一个简单的结构代码失败 [英] a simple struct code failing

查看:43
本文介绍了一个简单的结构代码失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>


嗨大师,

我是试图编译下面的代码,我在

行上收到错误标记为/ *这一行* /。

错误说,

预期表达式''节点''"

有人可以解释一下原因吗?

struct node

{

int data;

struct node * next;


};


typedef struct node node;

node * make_n(node * nodeptr,int n)

{

int i = 0;

node * newnode = NULL;

/ *请检查以下行* /

nodeptr =(node *)malloc(sizeof node); / *这一个* /

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;


}

返回firstnode;

}

#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;
}

推荐答案



" saurabh" < sa ***** @ nowhere.nodomainwrote in message

news:ga ********** @ aioe.org ...

"saurabh" <sa*****@nowhere.nodomainwrote in message
news:ga**********@aioe.org...

#include< stdio.h>

#include< stdlib.h>


嗨大师,

我正在尝试编译以下代码,而我收到错误



行标有/​​ *这一行* /。

错误说,

预期表达式''节点''

有人可以解释一下原因吗?
#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); / *这一个* /
nodeptr=(node*)malloc(sizeof node);/*This one*/



将sizeof节点更改为sizeof(节点)。更进一步向下几行。

我认为sizeof期望括号围绕类型参数。


-

Bartc

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

--
Bartc


9月12日12:12 pm,saurabh< saur ... @ nowhere.nodomainwrote:
On Sep 12, 12:12 pm, saurabh <saur...@nowhere.nodomainwrote:

#include< stdio.h>

#include< stdlib.h>


嗨大师,

我正在尝试编译以下代码,并且我在

行上收到错误标记为/ *这一行* /。

错误说,

"节'''之前的预期表达式'

有人可以解释一下原因吗?

struct node

{

int data;

struct node * next;


};


typedef struct node node;


node * make_n(node * nodeptr,int n)

{

int i = 0 ;

节点* newnode = NULL;

/ *请检查以下行* /

否deptr =(node *)malloc(sizeof node); / *这一个* /
#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*/



在类型名称上使用sizeof运算符时,类型名称必须为<括号内的



nodeptr = malloc(sizeof(node));


就个人而言,我更喜欢打电话正在分配的对象上的sizeof

(请注意,对象的名称在括号中不是*):


nodeptr = malloc(sizeof * nodeptr) ;


这对我来说有点简单。


[snip rest]

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]

saurabh< sa ***** @ nowhere.nodomainwrites:

[...]
saurabh <sa*****@nowhere.nodomainwrites:
[...]

我正在尝试编译以下代码,我在

行上收到错误标记为/ *这一行* /。

错误说,

'''节点''之前的预期表达式'

有人可以解释一下原因吗?

struct node

{

int data;

struct node * next;


};


typedef struct node node;
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); / *这一个* /
nodeptr=(node*)malloc(sizeof node);/*This one*/



[...]


sizeof运算符有两种形式:" sizeof expr"由于节点是类型名称,因此sizeof(类型)和sizeof(类型)。

需要是sizeof(节点)。


但是有更好的方法来写整行:


nodeptr = malloc(sizeof * nodeptr);


转换malloc的结果是不必要的,并且在某些情况下可以掩盖

中的错误。并且通过使用sizeof * nodeptr,该行保持正确

,即使nodeptr稍后更改为其他类型。 (不要担心

关于取消引用未初始化的指针; sizeof

的参数未被评估。(可变长度数组有例外;

这里不用担心你。))


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

Nokia

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

[...]

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"


这篇关于一个简单的结构代码失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆