结构指针的语法 - 链表 [英] Syntax of pointers to structures - linked lists

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

问题描述

我学习电子工程,并将 C 作为我的第一语言学习.我一直在学习 IIT C Programming 课程,当涉及到指针时,我通常需要多花一点时间来理解程序.

我现在正在学习链表,我会放一部分代码然后告诉你们我有什么问题.我不会把所有的东西都放进去,因为我认为没有必要.

typedef struct node_type {整数数据;struct node_type *next;} 节点;typedef node_type *list;列表头,温度;字符 ch;国际n;头 = NULL;scanf("%c", &ch);while (ch == 'Y' || ch == 'y') {temp = (list)malloc(sizeof(node));温度->数据= n;温度->下一个=头;头 = 温度;scanf("%c", &ch);

我不明白为什么在处理指向结构的指针时使用->",我现在知道 temp->data = n; 等价于 (*temp).data = n;.但是我对第二个表达式的语法有疑问,为什么使用它可以访问temp"指向的结构内的数据".

在声明变量时,这里有一个顺序:http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

我知道运算符有优先顺序.在声明中,我将 * 读作指向".我应该如何读取代码中间的(*temp).data(不是声明)?

在课程开始时,PPChakraborty 博士说 * 可以理解为the content of",但在这个表达中没有意义:the content of temp..."

  • 未在声明中使用时运算符 * 的含义是什么?
  • typedef 的使用遵循与 int *a[] 相同的声明规则?typedef int *a[] 声明类型 'a' 是一个指向 int 的指针数组?

感谢任何帮助.

解决方案

我知道运算符有优先顺序.在声明中,我将 * 读作指向".我应该如何读取代码中间的 (*temp).data(不是声明)?

检索temp 指向的struct node_type 实例的data 成员".

图形化:

 +---+ +---+温度: ||----->||数据 <--- 我想要这个+---+ +---+||下一个+---+

<块引用>

但是我对第二个表达式的语法有疑问,为什么使用它可以访问temp"指向的结构内的data".

表达式 temp 的类型为指向 struct node_type 的指针".一元 * 运算符 取消引用 访问指向对象的指针.因此,表达式 *temp 的类型为 struct node_type.

假设有以下声明:

struct node_type 实例;struct node_type *pointer = &instance;

两个声明都完成后,以下表达式为真:

 指针 == &instance;//struct node_type * == struct node_type **指针==实例;//结构体节点类型 == 结构体节点类型指针->数据 == (*pointer).data == instance.data;//int == int == int指针->下一个 == (*pointer).next == instance.next;//struct node_type * == struct node_type *

成员选择运算符 .-> 都与后缀运算符分组,后缀运算符的优先级高于一元 * -如果您编写了 *temp.data,编译器会将其解析为 *(temp.data),这意味着我想要 temp.数据指向."

图形化:

 +---+ +---+温度: ||数据------>||<--- 我想要这个+---+ +---+||下一个+---+

在这种特殊情况下,这不是您想要的.

如您所见,temp->data 等价于 (*temp).data - -> 运算符 隐式取消引用temp指针.当处理指向 structunion 类型的指针时,你真的想使用 -> - 它不那么刺眼,而且你不太可能犯错误.

其他需要注意指针优先级问题的地方:

T *a[N];//a 是指向 T 的指针的 N​​ 元素数组

图形化:

 +---+ +---+一个: ||[0] ---->||T 的一些实例+---+ +---+||[1] --++---+ |+---+... +->||T 的其他一些实例+---+

每个 a[i] 指向一个不同的 T 类型的对象.要访问该对象,您需要取消引用数组元素 - *a[i].

T (*a)[N];//a 是一个指向 T 的 N 元素数组的指针

图形化:

 +---+ +---+一个: ||----->||(*a)[0]+---+ +---+||(*a)[1]+---+---

要访问数组的元素,您需要在应用下标之前引用指针 a - 您想要索引到 a 指向,所以需要写(*a)[i].

T *f();//f 是一个函数,返回一个指向 T 的指针

函数f返回一个指针值;要访问所指向的事物,您需要遵循返回值:

x = *f();

请注意,这是很少完成的;您总是希望对返回的指针值进行完整性检查.您更有可能看到:

T *p = f();如果 ( p )x = *p;

最后,

T (*f)();//f 是一个指向返回 T 的函数的指针

f 指向一个函数 - 您必须取消引用 f 本身才能执行该函数:

x = (*f)();

您可以根据af 的声明获得如何使用 的线索.

I study electronics engineering and I'm learning C as my first language. I've been following IIT C Programming course, and it often takes me a little more time to understand the program when pointers are involved.

I'm learning about linked lists right now, I'll put part of the code then tell you guys what I'm having problem with. I'll not put everything because I don't think it will be necessary.

typedef struct node_type {
    int data;
    struct node_type *next;
} node;

typedef node_type *list;

list head, temp;
char ch;
int n;
head = NULL;
scanf("%c", &ch);

while (ch == 'Y' || ch == 'y') {
    temp = (list)malloc(sizeof(node));
    temp->data = n;
    temp->next = head;
    head = temp;
    scanf("%c", &ch);

I couldn't understand why the use of '->' when working with pointers to structs, I know now that temp->data = n; is equivalent to (*temp).data = n;. But I'm having problem with the syntax of the second expression and why using it gives access to 'data' inside the struct which 'temp' points to.

When declaring variables there's an order as seen here: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

I know that there's a precedence order for the operators. In declarations I read * as "pointer to". How should I read (*temp).data in the middle of the code (not declaration) ?

In the beginning of the course, Dr.P.P.Chakraborty says that * could be understand as "the content of", but it wouldn't make sense in this expression: "the content of temp..."

  • What is the meaning of the operator * when not used in declarations?
  • The use of typedef follows the same rule of declarations like int *a[]? typedef int *a[] declares that the type 'a' is an array of pointers to int?

Appreciate any help.

解决方案

I know that there's a precedence order for the operators. In declarations I read * as "pointer to". How should I read (*temp).data in the middle of the code (not declaration) ?

"Retrieve the data member of the struct node_type instance that temp points to".

Graphically:

      +---+        +---+
temp: |   |----->  |   | data <--- I want this
      +---+        +---+
                   |   | next
                   +---+

But I'm having problem with the syntax of the second expression and why using it gives access to 'data' inside the struct which 'temp' points to.

The expression temp has type "pointer to struct node_type". The unary * operator dereferences the pointer to access the pointed-to object. Thus, the expression *temp has type struct node_type.

Assume the following declarations:

struct node_type instance;
struct node_type *pointer = &instance;

After both declarations are complete, the following expressions are true:

 pointer == &instance;    // struct node_type * == struct node_type *
*pointer ==  instance;    // struct node_type   == struct node_type

 pointer->data == (*pointer).data == instance.data; // int == int == int
 pointer->next == (*pointer).next == instance.next; // struct node_type * == struct node_type *

Both of the member selection operators . and -> are grouped with the postfix operators, which have higher precedence than unary * - had you written *temp.data, the compiler would have parsed it as *(temp.data), which would mean "I want the thing that temp.data points to."

Graphically:

      +---+              +---+
temp: |   | data ------> |   | <--- I want this
      +---+              +---+
      |   | next
      +---+

That's not what you want in this particular case.

As you discovered, temp->data is equivalent to (*temp).data - the -> operator implicitly dereferences the temp pointer. When dealing with pointers to struct and union types, you really want to use -> - it's less eye-stabby, and you're less likely to make a mistake with it.

Other places to watch out for precedence issues with pointers:

T *a[N];   // a is an N-element array of pointers to T

Graphically:

   +---+            +---+
a: |   | a[0] ----> |   | some instance of T
   +---+            +---+
   |   | a[1] --+
   +---+        |   +---+
    ...         +-> |   | some other instance of T
                    +---+

Each a[i] points to a distinct object of type T. To access the object, you'd dereference the array element - *a[i].

T (*a)[N]; // a is a pointer to an N-element array of T

Graphically:

   +---+        +---+
a: |   | -----> |   | (*a)[0]
   +---+        +---+
                |   | (*a)[1]
                +---+
                 ---

To access elements of the array, you need to deference the pointer a before you apply the subscript - you want to index into the thing a points to, so you need to write (*a)[i].

T *f();   // f is a function returning a pointer to T

The function f returns a pointer value; to access the thing being pointed to, you'd need to deference the return value:

x = *f();

Note that this is rarely done; you always want to do a sanity check on returned pointer values. You're far more likely to see:

T *p = f();
if ( p )
  x = *p;

And finally,

T (*f)(); // f is a pointer to a function returning T

f points to a function - you must dereference f itself in order to execute the function:

x = (*f)();

You can get a clue of how to use each version of a and f based on its declaration.

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

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