webdancer's Blog
c函数指针
//声明整数相加函数 int add_int(int a, int b) { return a + b; } //声明浮点数相加函数 float add_float(float a, float b) { return a+b; } //定义函数指针类型 typedef int (*int_add_funtion)(int a,int b); //定义函数指针类型 typedef float (*float_add_funtion)(float a , float b); int main() { //声明一个函数指针,名字叫做int_add_fun,相当于创建int_add_funtion这个类型的实例,下同 int_add_funtion int_add_fun; //声明一个函数指针,名字叫做float_add_fun float_add_funtion float_add_fun; //将该函数指针指向 整数相加函数 的入口地址 int_add_fun = add_int; //将该函数指针指向 浮点数相加函数 的入口地址 float_add_fun = add_float; //以常规方式调用 整数相加函数,打印出100+200的计算结果 printf("%d\n", add_int(100,200)); //用函数指针调用 整数相加函数,打印出100+200的计算结果 printf("%d\n", (*int_add_fun)(100,200)); //以常规方式调用 浮 点数相加函数,打印出100.32 + 324.54的计算结果 printf("%f\n", add_float(100.32f, 324.54f)); //用函数指针调用 浮点数相加函数,打印出 100.32 + 324.54的计算结果 printf("%f", (*float_add_fun)(100.32f, 324.54f)); return 0; }
重新拾起c——符号
以上为符号的列表。对符号我们就是知道他们经常用的意思!
注意:
1)/* :是注释号,编译器会自动寻找*/与之匹配。int i = 8; int * pi=&i; int j=i/*pi; j的定义有问题。应为:j=i/(*pi);
2)对一个新手来说,注释可能很重要!要写好。
3)\:持续符和转义符。
转义字符转义字符的意义
\n 回车换行
\t 横向跳到下一制表位置
\v 竖向跳格
\b 退格
\r 回车
\f 走纸换页
\\ 反斜扛符"\"
\' 单引号符
\a 鸣铃
\ddd 1~3 位八进制数所代表的字符
\xhh 1~2 位十六进制数所代表的字符
4)逻辑运算符:
&& ,|| :自动判断,如果前面可以判定真假,后面就不理会了。
5)位运算符:
& 按位与
| 按位或
^ 按位异或
~ 取反
<< 左移
>> 右移
位运算符对底层编程很重要。
6)++,--。
有些人不建议使用++,--。因为很多时候,非常容易造成错误!其实,就是优先级问题。对优先级加括号应该是一个好方法!
8)优先级:
尽管我们可以通过加括号來控制,但是阅读他人的代码,还是很有用!
Operator(s) | Description | Associativity | |
17
|
:: | global scope (unary) |
right-to-left
|
17
|
:: | class scope (binary) |
left-to-right
|
16
|
-> . | member selectors |
left-to-right
|
16
|
[ ] | array index |
left-to-right
|
16
|
( ) | function call |
left-to-right
|
16
|
( ) | type construction |
left-to-right
|
16
|
sizeof | size in bytes |
left-to-right
|
15
|
++ -- | increment, decrement |
right-to-left
|
15
|
~ | bitwise NOT |
right-to-left
|
15
|
! | logical NOT |
right-to-left
|
15
|
+ - | unary plus, minus |
right-to-left
|
15
|
* & | dereference, address-of |
right-to-left
|
15
|
( ) | cast |
right-to-left
|
15
|
new delete | free store management |
right-to-left
|
14
|
->* .* | member pointer selectors |
left-to-right
|
13
|
* / % | multiplicative operators |
left-to-right
|
12
|
+ - | arithmetic operators |
left-to-right
|
11
|
<< >> | bitwise shift |
left-to-right
|
10
|
< <= > >= | relational operators |
left-to-right
|
9
|
== != | equality, inequality |
left-to-right
|
8
|
& | bitwise AND |
left-to-right
|
7
|
^ | bitwise exclusive OR |
left-to-right
|
6
|
| | bitwise inclusive OR |
left-to-right
|
5
|
&& | logical AND |
left-to-right
|
4
|
|| | logical OR |
left-to-right
|
3
|
? : | arithmetic if |
left-to-right
|
2
|
= *= /* %= += -= |
assignment operators |
right-to-left
|
1
|
, | comma operator |
left-to-right
|
当然,得经常调试代码!