简体   繁体   English

C语言中的语法理解:指针

[英]Syntax understanding in C: Pointers

I have medium knowledge working with pointers. 我有使用指针的中等知识。 Some of the syntax styles baffles me. 一些语法样式使我感到困惑。

Like: 喜欢:

  1. *(uint8 *) (a) = (b)
  2. typecasting the reference when parsing it in a function. 在函数中解析引用时进行类型转换。 The function is prototyped as func(unsigned char * a); 该函数原型为func(unsigned char * a); but it is used as func((unsigned short *) &b); 但它用作func((unsigned short *) &b); .... As far I understand they were casting a expected char type pointer into a short. ....据我了解,他们正在将预期的char类型的指针转​​换为short。

Can anyone help me understand what these statements exactly mean in C? 谁能帮助我了解这些语句在C中的确切含义?

The first one simply casts (a) into a pointer to an unsigned 8-bit integer, then writes the result of evaluating (b) to the resulting address (as an 8-bit number). 第一个简单地将(a)转换为指向无符号8位整数的指针,然后将求值(b)的结果写入结果地址(作为8位数字)。

The second one isn't valid, it shouldn't compile. 第二个无效,不应编译。 Perhaps you mis-remember, it's a bit sketchy-looking. 也许您记错了,看起来有点粗略。

To understand what definitions mean in C take a look at the right-left rule, it's very simple and explains a lot http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html . 要了解C中的定义,请看一下左右规则,它非常简单,并解释了很多http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html What it basically means is that you start out with the name and move right building up the definition then move left only when you can't move right. 它的基本含义是,从名称开始,向右移动以建立定义,然后仅在无法向右移动时才向左移动。 This might sound weird but let's take an example with number 1. 这听起来可能很奇怪,但让我们以数字1为例。

  1. *(uint8 *) (a) = (b) *(uint8 *)(a)=(b)

    So step 1 what is a? 那么第一步是什么? Start with the name of the variable which in this case is a. 以变​​量名开头,在这种情况下为a。 Then look to see what is defined to the right of that, here it is nothing, so move to the part immediately to the right, which is (uint8 ). 然后看一下它右边的定义,这里什么也没有,所以直接移到右边的部分,即(uint8 )。 Which means that a is a pointer to a uint8. 这意味着a是指向uint8的指针。 Ok now move right from the uint8 and you get back to a so that means you can't move right anymore, so go right then which gets you *, which is the dereferencing a. 好的,现在从uint8处向右移动 ,然后返回到a,这意味着您无法再向右移动,因此,请继续向右移动,然后得到*,这是取消引用a。 So for this line the uint8 stored at the address a is set to the value of b. 因此,对于此行,将存储在地址a的uint8设置为b的值。

  2. Ok for the second part I think the func((unsigned short *) &b) is where they begin defining the function body? 好吧,第二部分我认为func((unsigned short *) &b)是他们开始定义函数体的地方? If that is case then that means for the function body the passed in parameter is used as a pointer to an unsigned short, but if the function is called like this I think the compiler wouldn't be happy because of the type mismatch since the function is expecting an unsigned char * not an unsigned short * . 如果是这种情况,则意味着对于函数主体,传入的参数用作指向无符号short的指针,但是如果这样调用函数,则我认为由于类型不匹配,导致编译器不满意,因为该函数期望一个unsigned char *不是一个unsigned short *

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

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