Functions
库函数中的字符串处理:
http://blog.csdn.net/general1982/article/details/4012566
函数好处:增强代码复用性
函数最多给用户返回一个数据 即:return
函数定义格式:
return-type function-name(argument declarations)
//argument declarations参数声明
{
declarations and statements//函数体
}
Three places where a function name is used:
1. Declaration (Prototype)//声明时参数名可以省略
2. Call (Invocation) //函数名(参数列表);
3. Definition (Specification)
实参:函数调用时的参数。
形参:不是函数调用时的参数。
Functions : Parameters
参数传递:
1. Pass by value 值传递(是一种单向传递,只能由实参->形参)
2. Pass by address 传地址,传指针
3. Pass by reference 传引用
int foo(int a, int b = 1, int c = 2)//形参有默认值是必须是自右向左依次有默认值
eg:int foo(int a, int b = 1, int c)//会报错
Inline functions内联函数(用实现替代函数调用这句话)(以空间换时间)
Eg:
inline int func( int a, int b )
{
return a+b;
}
Function overloading 重载(函数名相同,参数列表不同(参数个数不同,参数类型不同))(c语言没有重载)
Pointers(指针)
Pointer Variables:指针变量特点
– Contain an address//存储地址
– Can have value changed//值可以变化(“指向”可以更改)
– “Point” to a specific type of data//需要指向“特定”类型的数据eg:int* a;char* b;
–Many variables can point to the same value!//多个指针变量的“指向”可以相同
Accessing the Variables
访问变量
The Operator * is the dereference operator解地址(解引用)操作符 *
“Dereferencing” often read as “contents of”
int* p;//p指向不明确: 野指针:指向不明确的指针 危险的
p = NULL; //值为NULL的指针即为“空指针” 系统空间的地址编号0x0(无权访问)
*p 不能对空指针进行 解地址
拼接字符串手写(库中有函数strcat不需手写)
#include <stdio.h>
#include <string.h>
int main()
{
int i=0,j=0;
char str1[30];
gets(str1);
char str2[]="jintianchishenme";
while (str1[i])
i++;
while (str1[i++]=str2[j++]);
puts(str1);
return 0;
}