Study notes C (5) --- a second pointer part, string, structure.

1. The function pointer (function pointer):

 

Function pointer variable is a pointer to function.

 

Usually we say pointer variables are variables such as pointing to an integer, character, or array, and function pointer is a pointer to a function.

 

Function pointer can be as general as a function for calling the function, passing parameters.

 

Function pointer variable declaration:

typedef int (*fun_ptr)(int,int);

Examples: https://www.runoob.com/cprogramming/c-fun-pointer-callback.html

sub: pointer function returns a pointer to a function, as described in my last article.

 

2. callback (Callback function):

 

 

, The callback function is a function call through a function pointer function pointer variables can be used as a parameter to a function.

"

The following is a commentary from the author often know almost Creek Ling:

You go to a store to buy something, just something you do not want the goods, so you left your phone in the clerk there, after a few days the store stock, the clerk hit your phone, then you received a phone call after the store to pick up the goods. In this example, your number is called callback function, you have the telephone clerk left to register a callback function is called, the store later called the stock trigger event associated callback, call the clerk to call you callback function is called, you go to the store to pick up a callback event called the response.

"

In simple terms that means: A function of the parameters you are now contains a definition you do not know the B function, so you do a B function pointer in the argument to refer to the B function. Once cited the B function is defined, you can use your A function at runtime.

 

Examples: https://www.runoob.com/cprogramming/c-fun-pointer-callback.html

 

sub: size_t is a data type, similar to the unsigned integer, but the capacity is generally greater than the range of int and unsigned. As used herein, in order to ensure arraysize size_t variable to have a capacity large enough to store a large array may be.

 

 

 3. The string (string):

 

In the C language, using the string actually  null  character '\ 0' one-dimensional array of characters terminated. Therefore, a null-terminated string that contains the character string.

The following statements create and initialize a "Hello" string. Since the end of the array of stores null character, so more than one size of the array of characters than the number of characters in the word "Hello" is.

char greeting [6] = { ' H', 'e', 'l', 'l', 'o', '\ 0'}; ( normal people do not use this) (without the  null  character back when the string end of the constant array .C compiler initializes, automatically '\ 0' on the end of the string.)

 

char greeting [] = "Hello"; (more generic, and other languages ​​like, attention "[]", since the C char string is inside the array.)

 

Common operations:

strcpy (s1, s2); copy the string to the string s2 s1.

strcat (s1, s2); connection string s2.

strlen (s1); returns the length of the string of s1.

strcmp (s1, s2); If s1 and s2 are the same, then return 0; if s1 <s2 is less than 0 is returned; if s1> s2 is greater than 0 is returned.

the strchr (s1, ch); Returns a pointer to the location of the first character string s1 first occurring ch.

Strstr (s1, s2); Returns a pointer to the location of the string s1 s2 of the first occurrence of the string.

 

 

sub:

sizeof strlen and the difference between:

strlen 是函数,sizeof 是运算操作符,二者得到的结果类型为 size_t,即 unsigned int 类型。

sizeof 计算的是变量的大小,不受字符 \0 影响;

而 strlen 计算的是字符串的长度,以 \0 作为长度判定依据。

 

 4.结构体(struct):

 

a.定义:

 

C 数组允许定义可存储相同类型数据项的变量,结构是 C 编程中另一种用户自定义的可用的数据类型,它允许您存储不同类型的数据项

 

假设我们有一个电子图书馆,我们可以用结构体来定义一本书及其属性: 1.标题 2.作者。。。之类。

为了定义结构,您必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型,struct 语句的格式如下:

 

struct Books {

  char  title[50];

  char  author[50];

  char  subject[100];

  int   book_id;

}book;

 在一般情况下,tag 、member-list、variable-list 这 3 部分(对应代码里面的Books, char  title[50]; , book)至少要出现 2 个。

 

sub :

1.不同的定义方式产生的结构体可以被视为不同的类型,无法相等(不管成员是不是一样).

2.结构体的成员可以包含其他结构体,也可以包含指向自己结构体类型的指针,而通常这种指针的应用是为了实现一些更高级的数据结构如链表和树等。

3.可以在定义的时候初始化,跟变量一样。

 

 详见:https://www.runoob.com/cprogramming/c-structures.html

 

b. 结构访问还有使用:

 

为了访问结构的成员,我们使用成员访问运算符(.)。成员访问运算符是结构变量名称和我们要访问的结构成员之间的一个句号。您可以使用 struct 关键字来定义结构类型的变量。

 比如 你要访问 Book1 的title --- Book1.title   (如果学过面对对象编程的朋友估计感觉很眼熟)

 

c.结构作为函数参数:

 

您可以把结构作为函数参数,传参方式与其他类型的变量或指针类似。您可以使用上面实例中的方式来访问结构变量:

 

void printBook( struct Books book ){

。。。

。。。

。。。

}

 

d.指向结构的指针:

 

struct Books *struct_pointer;        定义

struct_pointer = &Book1;            结构变量的地址

struct_pointer->title;               指向该结构的指针访问结构的成员

 

e.位域:

 

有些信息在存储时,并不需要占用一个完整的字节,而只需占几个或一个二进制位。例如在存放一个开关量时,只有 0 和 1 两种状态,用 1 位二进位即可。为了节省存储空间,并使处理简便,C 语言又提供了一种数据结构,称为"位域"或"位段"。

所谓"位域"是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。这样就可以把几个不同的对象用一个字节的二进制位域来表示。

 

位域和结构体的定义基本上差不多,只不过需要多加 位域长度 。

 

struct bs{

   int a:8;

   int b:2;

   int c:6;

}data;

 

说明 data 为 bs 变量,共占两个字节。其中位域 a 占 8 位,位域 b 占 2 位,位域 c 占 6 位。

使用时这两种都可以:1. data.a   2. data->a

 

 

sub:

1.一个位域存储在同一个字节中,如一个字节所剩空间不够存放另一位域时,则会从下一单元起存放该位域。也可以有意使某位域从下一单元开始。

2.由于位域不允许跨两个字节,因此位域的长度不能大于一个字节的长度,也就是说不能超过8位二进位。如果最大长度大于计算机的整数字长,一些编译器可能会允许域的内存重叠,另外一些编译器可能会把大于一个域的部分存储在下一个字中。

3.位域可以是无名位域,这时它只用来作填充或调整位置。无名的位域是不能使用的.

4.位域在本质上就是一种结构类型,不过其成员是按二进位分配的。

5.结构体变量的首地址能够被其最宽基本类型成员的大小所整除。

6.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节(internal adding)。即结构体成员的末地址减去结构体首地址(第一个结构体成员的首地址)得到的偏移量都要是对应成员大小的整数倍。

 

 

 引用:https://www.runoob.com/cprogramming/c-structures.html

 

 

Guess you like

Origin www.cnblogs.com/cptCarlvon/p/11883131.html