结构帮助 [英] structs help

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

问题描述

您好我是C的新手。我正在尝试弄清楚如何在我的主程序中初始化

a struct。

结构在一个外部头文件中声明像这样...

typedef struct ln {

int key;

int data;

struct ln * next;

} listNode,* listNodePtr;


只是为了测试我的main方法我试图初始化

listNodePtr变量像这样的null ...


listNodePtr * head = NULL;


首先据我所知listNode和* listNodePtr是

类型为ln的变量。但是我也学到了,所以我想,

listNodePtr是struct ln的别名。所以我上面的声明

应该是合法的。是吗?


当我尝试将我的新listNodePtr变量*传递给我的

listInsert方法时........


listInsert(head,1,2);


它不起作用。它构建但是在运行时我的程序在listInsert方法中挂起

第一个if条件

试图确定listNodePtr * list是否为null。

即if(* list == NULL).....这是程序退出的地方。


方法定义看起来像......


void listInsert(listNodePtr * list,int key,int value)


我假设方法体是正确的,因为这段代码
我的教授给了我们



请在初始化期间帮助我做错了什么?

我如何使用结构定义如上所述?


提前致谢。

Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the
listNodePtr variable to null like this...

listNodePtr *head = NULL;

First as far as I understand listNode and *listNodePtr are
variables of type ln. However I have also learned, so I thought,
that listNodePtr is an alias to the struct ln. So my statement
above should be legal. Is It?

When I try to pass my new listNodePtr variable *head to my
listInsert method like........

listInsert(head,1,2);

it doesn''t work. It builds but at run time my program hangs
when inside the listInsert method the first if condition
tries to determine of the listNodePtr *list is null or not.
i.e. if( *list == NULL )..... this is where the program quits.

The method definition looks like...

void listInsert(listNodePtr *list, int key, int value)

I am assuming the body of the method is correct as this code
was given to us to use by my Prof.

Please help what am I doing wrong during my initialization?
How do I use structs defined like the one above?

Thanks in advance.

推荐答案

Dave Cooke写道:
Dave Cooke wrote:
您好我是C的新手。我正在试图弄清楚如何在我的主程序中初始化
一个结构。
结构在这样的一个外部头文件中声明...
typedef struct ln {
int key;
int data;
struct ln * next;
} listNode,* listNodePtr;

只是为了测试我的main方法我试图初始化
listNodePtr像这样变量为null ...

listNodePtr * head = NULL;
Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the
listNodePtr variable to null like this...

listNodePtr *head = NULL;




如果你避免使用typedef来指向结构,你赢了不要这个

错误。


你的意思是

listNode * head = NULL; / *更好* /



listNodePtr head = NULL; / *不太好* /



If you avoid using typedefs for pointers to structs, you won''t make this
error.

You mean either
listNode *head = NULL; /* better */
or
listNodePtr head = NULL; /* not so good */


2004年4月3日星期六22:36:05 -0600,Dave Cooke

< dc * ***@ee.umanitoba.ca>写道:
On Sat, 03 Apr 2004 22:36:05 -0600, Dave Cooke
<dc****@ee.umanitoba.ca> wrote:
您好我是C的新手。我正在试图弄清楚如何在我的主程序中初始化
一个结构。
声明结构在像这样的anouther头文件中...
typedef struct ln {
int key;
int data;
struct ln * next;
} listNode,* listNodePtr;
Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;




上面的陈述混合了typedef语法机制

声明和定义结构。 typedef

的最简单用法是:


typedef some-type identifier;


导致标识符某种类型指定的类型

的另一个名称。如果你想定义一个名为listNode的struct / instance /

和指向它的指针,你只需要省略

" typedef"上面那就是你会得到的(你不会有b $ b有任何typedef,所以以后定义另一个相同类型的结构

你' 'd'说:

struct in anotherOne;

)。


如果你想创建一个typedef(类型别名)你正在使用类型

的结构,你会做这样的事情:


typedef struct在{

/ *你所有的会员声明* /

}在;


我使用了与结构标签相同的typedef名称,因为

在我身上发生的个人风格;你可能会选择这样做,否则就是
,但这是完全合法的。


所以在那之后,你可以说:


在listNode中,* listNodeptr;


因为编译器会因为上面的

原始typedef语句而感到困惑,所以我'我会停在这里让你

根据这些信息重新解决问题。


祝你好运,

-leor



The statement above is mixing the "typedef" mechanism with the syntax
of declaring and defining structures. The simplest usage of typedef
is:

typedef some-type identifier;

which results in the identifier being another name for the type
specified by some-type. If you want to define a struct /instance/
named listNode and a pointer to it, you''d simply omit the word
"typedef" above and that would be what you''d get (and you wouldn''t
have any typedefs, so to later define another struct of the same type
you''d say:
struct In anotherOne;
).

If you want to create a typedef (type alias) for a struct of the type
you''re using, you''d do something like this:

typedef struct In {
/* all your member declarations */
} In;

I used the same name for the typedef as for the structure tag, because
that happens to me my own personal style; you might choose to do
otherwise, but this is perfectly legal.

So after that, you could just say:

In listNode, *listNodeptr;

Because the compiler would have been confused from the point of your
original typedef statement above on, I''ll just stop here and let you
approach the problem anew based on this info.

Good luck,
-leor


Dave Cooke写道:
Dave Cooke wrote:
您好我是C的新手。我正在试图弄清楚如何初始化
结构在我的主程序中。
结构在这样的一个头文件中声明...
typedef struct ln {
int key;
int data;
struct ln * next;
} listNode,* listNodePtr;

只是为了在我的main方法中测试我尝试将
listNodePtr变量初始化为null,就像这样......

listNodePtr * head = NULL;
Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the
listNodePtr variable to null like this...

listNodePtr *head = NULL;




如果你避免使用typedef指向结构,你就不会犯这个

错误。


你的意思是

listNode * head = NULL; / *更好* /



listNodePtr head = NULL; / *不太好* /



If you avoid using typedefs for pointers to structs, you won''t make this
error.

You mean either
listNode *head = NULL; /* better */
or
listNodePtr head = NULL; /* not so good */


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

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