owned this note
owned this note
Published
Linked with GitHub
# Linux Kernel 開發日誌 2020.3.9
###### tags: `linux2020` `K&R` `C`
# 摘要
* K&R 6.2 Structure and Functions
* ```canonrect()```: canonicalize coordinates of rectangle
* pointer to structure
* qualified type
* incomplete type
----
# 筆記
## canonicalize coordinates of rectangle
```cpp
/**
* func: canonrect() - canonicalize coordinates of rectangle
* @Description
* used to make sure that
* pt1 is at bottom - left corner,
* pt2 is at top - right corner.
* @r: a rect which data type is 'struct rect'
* Return: a rect which data type is 'struct rect'.
*/
/* Use macro to compare the value of a and b */
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) < (b) ? (a) : (b))
rect cononrect (struct rect r)
{
rect temp;
temp.pt1.x = min(r.pt1.x, r.pt2.x);
temp.pt1.y = min(r.pt1.y, r.pt2.y);
temp.pt2.x = max(r.pt1.x, r.pt2.x);
temp.pt2.y = max(r.pt1.y, r.pt2.y);
return temp;
}
```
> 自我提問: How to explain the code below?
> ```c
> #define min(a, b) ((a) < (b) ? (a) : (b))
> ```
### Conditional operator 用在 macro 的資料查詢
>嘗試解釋
>```c
> #define min(a, b) ((a) < (b) ? (a) : (b))
> ```
發現
>```c
> ((a) < (b) ? (a) : (b))
> ```
是 conditional operator
附上資料來源
#### ISO / IEC - 9899 6.5.15
**Conditional operator**
**Syntax:**
```c
conditional-expression:
logical-OR-expression ? expression : conditional-expression
```
**Constraints**
The first operand shall have scalar type.
One of the following shall hold for the second and third operands:
* both operands have arithmetic type;
* both operands have the same structure or union type;
* both operands have void type;
* both operands are pointers to ==qualified or unqualified versions of compatible types==;
* one operand is a pointer and the other is a null pointer constant; or
* one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void.
自我問答:
What is ```qualified type```, ```incomplete type``` ?
* qualified type ?
* **C99-6.7.3 Type qualifiers**
* Syntax
* const
* restrict
* volatile
* The properties associated with qualified types are meaningful only for expressions that are lvalues.
(Page 120).
* incomplete type ?
* **C99-6.2.5 Type**
* The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.)
* Types are partitioned into
* **object types**
* (types that fully describe objects),
* **function types**
* (types that describe functions), and
* **incomplete types**
* (types that describe objects but lack information needed to determine their sizes).
## K&R 6.2 pointer to structure
Why use structure pointer?
>It's more efficient rather than passing the whole structure.
Say that,
```pp``` is a pointer to a structure type ```struct point```,
* if ```pp``` points to a structure ```point```, then
* ```*pp``` is the structure.
* ```(*pp).x``` and ```(*pp).y ```are the members.
See the code:
```cpp
struct point origin, *pp;
pp = &origin;
printf("origin is (%d, %d)\n", (*pp).x, (*pp).y);
```
Note that in c precedence table:
* ```.``` : structure and union member access.
* order 1
* ```*``` : Indirection (dereference)
* order 2
So,
```(*pp).x``` : dereference pointer ```pp``` 後得到 structure ```origin```, 再用 ```.``` access 他的 member.
### ```->```: Structure and union member access through pointer
因為 pointer to structure 很常用,
所以可以用 ```->``` 來簡潔地表示 access structure member through pointer.
Assume p is a pointer to a structure.
**Syntax:**
```c
p -> member-of-structure
```
e.g.
```cpp
printf("origin is (%d, %d)\n", pp->x, pp->y);
```
Note that ```->``` is left associated.
So,
if we have:
```cpp
struct rect r, *rp = &r;
```
then,
```cpp
r.pt1.x
rp->pt1.x
(r.tp1).x
(rp->pt1).x
```
are equivalent.
<br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/>
----
# 總結