简体   繁体   English

C中的简单结构程序

[英]Simple Structs program in C

I'm new to programming so please bear with me. 我是编程的新手,所以请耐心等待。 Can anyone please explain what the last line of code is doing and what this entire block of code trying to do. 任何人都可以解释最后一行代码正在做什么以及整个代码块试图做什么。 I know the first block is creating a structure called node and adding two pointers *next and *prev to it. 我知道第一个块正在创建一个名为node的结构,并添加两个指针* next和* prev。 btw this is a part of a linked list program 顺便说一下,这是链表计划的一部分

struct node
 {
  char line[80];
  struct node *next,*prev;
 };

struct node *start=NULL,*temp,*temp1,*temp2,*newnode;

Thank you in advance. 先感谢您。

struct node *start=NULL,*temp,*temp1,*temp2,*newnode;

can be as 可以如此

struct node *start=NULL;
struct node  *temp;
struct node *temp1;
struct node *temp2;
struct node *newnode;

Now is it easy to understand..? 现在容易理解..?

The last line is creating 5 pointers of type struct node namely start which is pointing to NULL , temp , temp1 , temp2 and newnode . 最后一行是创建5个类型为struct node指针,即start ,它指向NULLtemptemp1temp2newnode

The whole block of code is actually creating a struct called node which contains an array of 80 characters, followed by pointers to next and previous. 整个代码块实际上是创建一个名为node的结构,它包含一个包含80个字符的数组,后跟指向next和previous的指针。 Hence it is creating a structure for a doubly linked list. 因此,它正在为双向链表创建一个结构。

The last line is creating 5 pointers of type struct node. 最后一行是创建5个struct struct节点的指针。 Here start is initilaized to Null. 这里的开始是初始化为Null。

The last line of code defines 5 variables, all of which are pointers to struct node . 最后一行代码定义了5个变量,所有变量都是struct node指针。 One of them, start , is initialized to NULL; 其中一个, start ,初始化为NULL; the others are also initialized to NULL if the variables are outside any function, but are uninitialized if they are inside a function. 如果变量在任何函数之外,其他的也被初始化为NULL,但如果它们在函数内部则未初始化。 Uninitialized pointers should be initialized before they are used. 未初始化的指针应在使用前初始化。 Null pointers should not be dereferenced; 空指针不应该被取消引用; madness lies at the end of a null pointer (or, more usually, a core dump). 疯狂位于空指针的末尾(或更常见的是核心转储)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM