数据类型和变量
32位机器:
// char 1//short 2//int 4//long//double//float//sizeof(a)/sizeof(a[0])
typedef 和结构体的配合
typedef struct c_class{int a;}c_s;c_s tmp;tmp.a = 1;
万能指针 void*
void 为无类型指针,也叫万能指针,可以指向任何类型的数据
void指针作为左值用于“接收”任意类型的指针
void*指针作为右值赋值给其它指针时需要强制类型转换
void *p = NULL ;//可以做函数返回值,也可以做参数,类似于c++的模板类编程void * memcpy(void *dest, const void *src, size_t len);char *p2 = (char *)malloc(sizoeof(char)*20);//使用时注意要转为相应的类型
内存四区(堆、栈、全局、代码区)
全局区、堆区、栈区、代码区


//注意事项:char *P ="acb123";//文字放在常量区,不能修改//假设有两个常量内容是一样的话,那么在全局区只会有一份空间,赋予的两个变量指向的地址是一样的char* get_str1(){char *p ="abc";return p;}char* get_str2(){char *p ="abc";return p;}int main(){char *p1 = get_str1();char *p2 =get_Str2();printf("%s",p1);printf("%s",p2);//打印的地址是一样的}
堆区栈区举例子
char* get_str3(){char buff[] ="abc";return buff;}int main(){char *p =get_str3();//err 会乱码}/*分析:abc 在常量区创建然后赋值给在栈区创建的buff,在运行主函数时,会将buff的首地址赋给p,但get_str3由于在栈区,已经被释放,所以会出错解决办法:使用 (char*)molloc(n)开辟空间,调用完后释放free()*/
指针强化
1、指针也是一种数据类型占有 4 个字节
//注意:int *p = NULL; //*号含义:定义int a = 10;p = &a;*P = 20;//*号含义:赋值,两处的*号含义是不同的
2、指针的步长
char *p = NULL;char buff[]="abcde";p=buf;//p指向首地址ap=p+1;//p指向 b//步长由类型决定
3、写内存时一定要确保指向的地址能够操作
char *buff ="abcdef";buff[1]= 'c';//error,在常量区不能修改char buff1[]="abcdef";buff1[1] = 'c';//可以修改,因为常量区的字符串copy到了buff1的区域
间接赋值(*p)是指针存在的最大意义
//返回多个值 形参改变实参必须要地址传递void tmp(int *a,int *b,int *c ){*a ++;*b ++;*c ++;}int main(){int a = 0,b = 0,c = 0;tmp(&a,&b,&c);}
二级指针间接赋值
比如说是一个函数形参中有两个 例如 `fun(char p),那么只有两种情况,<br />一是调用时传入的 bchar *b; fun(&b); 这是同级传递,属于值传递<br />二是调用时传入的 bchar b` ; fun(&b); 这是同差一级传递,属于地址传递
int a =0;int *p=&a;//保存a的地址int **q= &p;//保存p的地址//保存谁的地址,在原来的基础上多加一个*void tmp(**p){*P = 0x12;}void main(){int *p = 0x11;tmp(&p);}
字符串
初始化
char buff[]={'a','b'};printf("%s",buff);//error 乱码,因为不是以'\0'或0结束的char buff1[10]={'a','b'};printf("%s",buff);//yes 数组初始化会自动填0char buff3[10]={'a','0','b'};//=>a0bchar buff4[10]={'a',0,'b'};//=>achar buff5[10]={'a','\0','b'}; //=>a//常用初始化方式char buff6 ="abc";printf("%d,%d",strlen(buff6),sizeof(buff6));// => 3,4
调用访问
char buff[100] ="abcsefg";//数组方法调用int i=0;for(i=0;i<strlen(buff),i++){printf("%c",buf[i]);}//指针方法访问char *p = buff;for (i=0;i<strlen(buff),i++){printf("%c",*(p+i));}//当然也可以这样for (i=0;i<strlen(buff),i++){printf("%c",*(buff+i));}//但是buff和p是有区别的,buff 不能被改变(因为修改后,编译器就无法释放其内存了),p是一个指针变量,可以修改,如p++
字符串拷贝
int my_str(char* dst,char* src){//1、最好不要直接用形参char *to = dst;NULLchar *from = src;//2、判断是不是为空if(dst==NULL||src==NULL){return -1;}while(*to++=*from++);//=*dst++ = src++; ==> *dst = *src;dst++;src++ 当为0时退出。//i++ 先用后加return 0;}
const
int const *p;const int *p;//这两个是相等的 修饰的是*p,就是说不能改变指向的内存的值int * const p;// 修饰的是p,p是一个指针,就是说不能改变指针的指向typedef struct tmp{int a;}tmp;
指针数组
是数组,是一个存放指针的数组
char* p[]={"0000","1111"};//里面存放的是地址,数组char* *p={"0000","1111"};//error 指针只能是只想一块内存//二级指针做输入void A(char a[]); // ====> void A(char *a);//由上类推void B(char *a[]);//=====> void B(char **a);
数组指针
数组指针是指针,指向一个数组
//定义数组指针的三种方法//1、typedef int A[10];//A代表数组类型A b;//int b[10]; 数组类型变量A *p;// 数组类型定义的数组指针变量//2、typedef int (*P)[10];//数组指针类型P p;//数组指针变量//3、int (*p)[10];//一维数组的使用方法int a[10]={0};p = &a;//首元素地址//二维数组的使用方法int a[][4] = {1,2,3,4, 5,6,7,8, 9,10,11,12};int (*P)[4];p=a;// 首行地址,即第0行首地址
一维数组
char *str = NULL;// strcpy(str,"er");//出错,str为分配内存//修正str = (char *)malloc(20);strcpy(str,"er");//内存中存放erprintf("%s",str);if(str!=NULL){free(str);str = NULL;}
二维数组
步长(和指针之间的转换)
char a[][10]={"aaaaaaa","bbbbb"};//第一个方框可以为空,但必须初始化 a代表首行地址,+1跳10,注意和首元素地址步长的区别//二维数组做参数int A(char a[][]);int A(char **a);//error,步长不一样,这个是指针,四个步长//针对步长的讲解int a[][4] = {1,2,3,4, 5,6,7,8, 9,10,11,12};// a 代表第0行首地址// a+i 代表第i行首地址 ===> &a[i]// *a 代表首行首元素地址 ===> &a[0][0]// *(a+i) 代表第i行首元素地址 ===> &a[i][0]// *(a+i)+j 代表第i行第j列元素的地址 ===> &a[i][j]// *(*(a+i)+j) 代表第i行第j列元素 ===> a[i][j]
二维数组做函数参数
__ 数组做形参都会退化为指针,注意指针步长
int a[][4] = {1,2,3,4, 5,6,7,8, 9,10,11,12};//1、第一种int func(int a[][4]);func(a);//调用//错误示例int func(int **a);//error 关键在于步长不一致//2、数组指针int func(int (*a)[4]);fun(a);
结构体
//基本操作struct people{char name[10];int age;};struct people man1 = {"liming",20};//初始化printf("%c,\n %d",man1.name man1.age);typedef struct people{char name[10];int age;}P;P p1;P *p_man = NULL:p_man = &p1;p_man -> name = "ha";p_man -> age = 20;
结构体嵌套结构体
typedef struct A{int a;}A;typedef struct B{int num;A a;}B;//oktypedef struct B{int num;B b;//error}B;//分析: 结构体还没有初始化成功,编译器不知道为B分配多大的内存typedef struct B{int num;B *next;///ok}B;//分析:指针内存大小是固定的
结构体嵌套一维数组
//结构体嵌套一维数组/** 一共是三种* 1、* people p1;* strcpy(p1.name,"er");** 2、* people *p=NULL;* strcpy(p->name,"er");** 3、* people *p2=NULL;* people p2[];**/void test02(){// 第一种people p1;p1.name = (char*)malloc(30);strcpy(p1.name,"er");p1.age = 10;printf("p1 %s, %d\n",p1.name,p1.age);if(p1.name != NULL){free(p1.name);p1.name = NULL;}//第二种people *p2=NULL;p2 =(people *)malloc(sizeof(people));p2->name = (char*)malloc(30);strcpy(p2->name,"lxf");p2->age = 26;printf("p2 %s, %d\n",p2->name,p2->age);if(p2->name !=NULL){free(p2->name);p2->name =NULL;}if(p2!=NULL){free(p2);p2 = NULL;}//第三种people *p3 = NULL;p3 = (people*)malloc(sizeof(people)*3);int i = 0;char buff[20];for (i=0;i<3;i++){p3[i].name = (char *)malloc(30);// fprintf(buff,"test %d",i);strcpy(p3[i].name ,"buff");p3[i].age = 20+i;}for(i=0;i<3;i++){printf("%s age: %d\n",p3[i].name,p3[i].age);}for(i=0;i<3;i++){if(p3[i].name!=NULL){free(p3[i].name );p3[i].name=NULL;}}if(p3!=NULL){free(p3);p3=NULL;}}
结构体嵌套二维指针
//每个老师三个学生typedef struct Tech{char **stu;// char *stu[];}tech;void test03() {//1、tech t1;int i = 0;t1.stu = (char **) malloc(sizeof(char *));for (i = 0; i < 3; i++) {t1.stu[i] = (char *) malloc(sizeof(char));strcpy(t1.stu[i], "ll");printf("%s\n", t1.stu[i]);}for (i = 0; i < 3; i++) {if (t1.stu[i] != NULL) {free(t1.stu[i]);t1.stu[i] = NULL;}}if (t1.stu != NULL) {free(t1.stu);t1.stu = NULL;}//2 tech *t2tech *t2 = NULL;t2 = (tech *) malloc(sizeof(tech));t2->stu = (char **) malloc(sizeof(char *));//t2->stu[i]for (i = 0; i < 3; i++) {t2->stu[i] = (char *) malloc(sizeof(char));strcpy(t2->stu[i], "xx");printf("%s\n", t2->stu[i]);}for (i = 0; i < 3; i++) {if (t2->stu[i] != NULL) {free(t2->stu[i]);t2->stu[i] = NULL;}}if (t2->stu != NULL) {free(t2->stu);t2->stu = NULL;}free(t2);t2 = NULL;//3 tech *t3; t3[];int j =0;tech *t3 = NULL;t3 = (tech *) malloc(sizeof(tech) * 3);for (i = 0; i < 3; i++){t3[i].stu = (char **) malloc(sizeof(char *));for (j = 0; j < 3; j++) {t3[i].stu[j] = (char *) malloc(sizeof(char));strcpy(t3[i].stu[j], "ff");printf("%s\n", t3[i].stu[j]);}for (j = 0; j < 3; j++) {if (t3[i].stu[j] != NULL) {free(t3[i].stu[j]);t3[i].stu[j] = NULL;}}if (t3[i].stu != NULL) {free(t3[i].stu );t3[i].stu = NULL;}}if(t3!=NULL){free(t3);t3 =NULL;}}
单向链表
静态链表
typedef struct node
{
int id;
char name[10];
struct node *next;
}node;
int main()
{
node n1 = {1,"aa",NULL};
node n2 = {2,"bb",NULL};
node n3 = {3,"cc",NULL};
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
//遍历
node *p = &n1;
while(p!=NULL)
{
printf("id:%d,name:%c",p->id,p->name);
p = p->next;
}
return 0;
}
函数指针
//和数组指针类似,最常用的方法如下
int func(int a)
{
printf("a = %d",a);
}
//主函数内
int (*pfunc)(int a) = func(2);// 结果 a = 2
//回调函数,函数指针的应用
int add(int x, int y)
{
return a+b;
}
int multi(int x, int y)
{
return x*y;
}
int fun(int x, int y ,int (*P)(int x,int y))
{
int tmp =0;
tmp = p(x,y);
return tmp
}
//主函数调用
int a = fun(1,2,add);
int b =fun(1,2,multi);
