C基础点总结

来源:互联网 发布:u盘数据剪切如何恢复 编辑:程序博客网 时间:2024/05/21 10:43
#指针入门
---


##C/C++ 指针入门


```c
/**
*指针本身也是在栈中,当指针指向某一个变量的地址的时,p的值就是变量a的地址,*p就是变量a所在地址的值.
*
*/
int a=2;
int *p=&a;//将


```


##指针为什么要有类型、指针与地址的区别、空指针、二级指针,指针运算、指针与数组


```c
/**
*因为有不同类型的变量(如int,double,char等),所以要存在不同的指针类型,以防内存不足,丢失精度等问题,
例如 
*
*/
double a=2; //double类型是8字节
int *p=&a;//int类型的内存是4个字节,而double需要8个字节的内存,所以会报错.


```


```c
/**
*二级指针,
*
*/
int a=4;
int *p=&a;//一级
int **p2=&p//二级指针 :p2上保存了&p得地址
**p2 = 90;//将p2的值,即p的指向的地址改为90
```
```c
//通过指针给数组赋值


void main(){
int uids[5];
//高级写法
//int i = 0;
//for (; i < 5; i++){
// uids[i] = i;
//}
//早些版本的写法
int* p = uids;
printf("%#x\n",p);
int i = 0; //i是数组元素的值
for (; p < uids + 5; p++){
*p = i;
i++;
}


getchar();
}
```






##指针与二维数组、函数指针,指针与二维数组、函数指针


```c
//函数指针
//用随机数生成一个数组,写一个函数查找最小的值,并返回最小数的地址,在主函数中打印出来
int* getMinPointer(int ids[], int len){
int i = 0;
int* p = &ids[0];
for (; i < len; i++){
if (ids[i] < *p){
p = &ids[i];
}
}
return p;
}


void main(){
int ids[10];
int i = 0;
//初始化随机数发生器,设置种子,种子不一样,随机数才不一样
//当前时间作为种子 有符号 int -xx - > +xx
srand((unsigned)time(NULL));
for (; i < 10; i++){
//100范围内
ids[i] = rand() % 100;
printf("%d\n", ids[i]);
}


int* p = getMinPointer(ids, sizeof(ids) / sizeof(int));
printf("%#x,%d\n",p,*p);
getchar();
}


```




##字符串相关函数
```c
//在线API文档:
//http://www.kuqin.com/clib/string/strcpy.html
void main(void){
char dest[50];
char *a = "china";
char *b = " is powerful!";
strcpy(dest,a);
strcat(dest,b);
printf("%s\n",dest);


system("pause");
}
```






##结构体、联合体、枚举
1.结构体
```c
//结构体是一种数据类型,把不同数据类型整合起来的一种自定义类型
struct Man{
char name[10];
int age;
}m1,m2,*Man_P;//m1,m2结构体变量名,*Man_p是结构体的指针变量


```
2.typedef 类型取别名
```c
//2.不同情况下,使用不同的别名
//#if defined(__cplusplus)
//typedef _JNIEnv JNIEnv;
//typedef _JavaVM JavaVM;


struct Man{
char name[10];
int age;
}m1,m2,*Man_P;//m1,m2结构体变量名,*Man_p是结构体的指针变量
//简写
typedef struct Woman{
char name[20];
int age;
} W, *WP;  //W 是woman结构体的别名, WP 是woman结构体指针的别名
```
3.结构体函数指针成员
```c
typedef struct Girl{
char *name;
int age;
//函数指针
void(*sayHi)(char*);
}Girl;


//Girl结构体指针取别名GirlP
typedef Girl* GirlP;


void sayHi(char* text){
MessageBoxA(0, text, "title", 0);
}


//改名
void rename(GirlP gp1){
gp1->name = "Lily";
}


void main(){
Girl g1 = { "Lucy", 18, sayHi };
GirlP gp1 = &g1;
gp1->sayHi("Byebye!");
//传递指针,改名
rename(gp1);


getchar();
}
```
4.联合体
```c
//联合体即共同体,不同类型的变量共用一段内存,而且任何时刻只有一个成员存在,节省内存
//联合体变量的大小=最大的成员所占的字节数
union Value{
int x;
int y;
double z;


}


```
4.枚举(列举所有情况)
```c
enum Day
{
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
};


```


##IO流
参考网 http://ganquan.info/standard-c/function/tmpnam


文件加密解密
```c
//二进制文件加解密
//读取二进制文件中的数据时,一个一个字符读取
//密码:ilovely


void crpypt(char normal_path[], char crypt_path[],char password[]){
//打开文件
FILE *normal_fp = fopen(normal_path, "rb");
FILE *crypt_fp = fopen(crypt_path, "wb");
//一次读取一个字符
int ch;
int i = 0; //循环使用密码中的字母进行异或运算
int pwd_len = strlen(password); //密码的长度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//写入(异或运算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);
}


//解密
void decrpypt(char crypt_path[], char decrypt_path[],char password[]){
//打开文件
FILE *normal_fp = fopen(crypt_path, "rb");
FILE *crypt_fp = fopen(decrypt_path, "wb");
//一次读取一个字符
int ch;
int i = 0; //循环使用密码中的字母进行异或运算
int pwd_len = strlen(password); //密码的长度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//写入(异或运算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);


}


void main(){
char *normal_path = "";
char *crypt_path = "";
char *decrypt_path = "";


//crpypt(normal_path, crypt_path,"iloveqq");
//解密
decrpypt(crypt_path, decrypt_path,"iloveqq");


getchar();
}




```
##C部分总结
  好久没写C的代码了,有些生疏.这几天把它捡起来,为JNI开发做准备.
  
0 0
原创粉丝点击