c++2017-6-22语句

来源:互联网 发布:mysql front 编辑:程序博客网 时间:2024/06/05 10:13

第9章 语句

选择语句:

if语句

if...else..语句

switch语句

循环语句:

while

do..while

for

退出循环:break return goto throw

注释与缩进


第10章 表达式

第12章 函数

12.1 函数声明:函数名字、返回值类型、参数数量和类型

12.1.1 为什么使用函数:把计算任务分解为易于理解的小块(表示成函数或者是类)

函数的规模应该较小,一眼就能知道该函数的所有信息。

程序员应该把函数视为代码的一种结构化机制。

12.1.2 函数声明的组成要件

12.1.3 函数定义

函数定义是特殊的函数声明,它给出了函数体的内容。

12.1.4 返回值

12.1.8 局部变量

12.2 参数传递

12.3 重载函数

为不同数据类型的同一种操作起相同的名字称为重载。

12.5 函数指针

//#include <string.h>
//#include <stdio.h>
//#define PP int*    //宏,PP代表int*
//typedef int* PINNT;
//
//int funcA(int a, int b); // function 1 statement
//int funcB(int* a, int* b);  //function 2 statement
//
//int main(){
// int(*func)(int, int);  //定义一个函数指针变量,可以接受函数名或函数名取地址
// //func = &funcA;
// func = funcA;
// //printf("%d", func(1, 10));
// printf("%d", (*func)(1, 10));
// //上述两种赋值方法和两种调用方法都可以,可以任选一种组合
// return 0;
//}
//int funcA(int a, int b){
// return a + b;
//}


#include <stdio.h>
int inc(int a){
 return(++a);
}

int multi(int* a, int* b, int* c){
 return(*c = *a**b);
}

typedef int(FUNC1)(int);             //定义了数据类型,这种类型声明的变量用于保存函数指针
typedef int(FUNC2)(int*, int*, int*);

void show(FUNC2 fun, int arg1, int* arg2){
 FUNC1 *p = &inc; //定义一个函数指针
 int temp = p(arg1); //调用inc函数
 fun(&temp, &arg1, arg2); //调用multi函数
 printf("%d\n",*arg2);  //输出结果
}

int main(){
 int a;
 show(multi, 10, &a);
 getchar();   //等待输入下一个字符
 return 0;  //结果为110,没啥问题
}

12.6 宏







原创粉丝点击