1.变量与地址
2.指针与指针变量
3.直接访问和间接访问
4.空指针与野指针
5.空类型
6.定义与初始化的书写规则
7.指针运算
8.指针与数组
指针与一维数组
指针与二维数组
指针与字符数组
9.const与指针
10.指针数组和数组指针
11.多级指针
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 1;
int *p;
p = &i;//int *p = &i; *是跟着int走的,类型是 int * 名字叫p(合法的标识符)
float *q;
double *d;
char *c;
printf("%d\n",sizeof(p));
printf("%d\n",sizeof(q));
printf("%d\n",sizeof(d));
printf("%d\n",sizeof(c));
//指针在某个平台下占用的大小是确定的
#if 0
printf("%d\n",sizeof(i));
printf("%d\n",sizeof(p));
printf("i = %d\n",i);
printf("&i = %p\n",&i);
printf("p = %p\n",p);
printf("&p = %p\n",&p);
printf("*p = %d\n",*p);
#endif
exit(0);
}
野指针
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p = NULL;//空指针
void *q = NULL;//void 类型的指针 不确定现在需要什么类型的指针
exit(0);
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 1;//空指针
int *p = &i;//void 类型的指针 不确定现在需要什么类型的指针
int **q = &p;
// i *p **p
//&i p *q
printf("i = %d\n",i);
printf("&i = %p\n",&i);
printf("p = %p\n",p);
printf("&p = %p\n",&p);
printf("*p = %d\n",*p);
printf(" q= %p\n",q);
printf("*q = %p\n",*q);
printf("**q = %d\n",**q);
exit(0);
}
指针运算
& * 关系运算 ++ –
指针与一维数组
#include<stdio.h>
#include<stdlib.h>
int main()
{
//a[i]: a[i] = *(a+i) = *(p+i) = p[i]
//&a[i]: &a[i] = a+i = p+i = &p[i]
//a 和 p 的区别 a是数组名是常量 p是指针变量 a++ 报错 p++ 不报错
//*a = a[0]
//p++ p=p+1; p的指向发生变化
//p+1 p的指向没变
//数组名是对某连续空间的抽象
int a[3] = {1,2,3};
int *p = a;
int i;
//for(i = 0;i <sizeof(a)/sizeof(*a);i++)
for(i = 0;i <sizeof(a)/sizeof(a[0]);i++)
{
printf("%p-->%d\n",&a[i],a[i]);
printf("%p-->%d\n",a+i,a[i]);
printf("%p-->%d\n",p+i,a[i]);
printf("%p-->%d\n",p+i,*(p+i));
}
printf("\n");
exit(0);
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[] = {5,7,8,2,3};
int y;
int *p = &a[1];;
y = (*--p)++; //(*--p) = (*--p) + 1 先赋值 再自增
printf("y = %d\n",y); //5
printf("a[0] = %d\n",a[0]); //6
exit(0);
}
指针与二维数组
int main()
{
int a[2][3] = {1,2,3,4,5,6};
int i,j;
int *p;
printf("%p %p\n",a,a+1);
// int *p; p = a; waring 行指针a 取* 相当于降级为列指针 *(a+i)+j
// p = *(a) 或者 p = &a[0]b[0] a降级为列指针 p 只能在列上移动
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
printf("%p->%d\n",&a[i][j],a[i][j]);
printf("%p->%d\n",*(a+i)+j,*(*(a+i)+j));
}
printf("\n");
}
}
数组指针:存储类型 数据类型 (*指针名【下标】) = 值
如 int (*p)[3]; -> type name; -> int[3] *p;
int main()
{
int a[2][3] = {1,2,3,4,5,6};
int i,j;
int *p = *a;
int (*q)[3] = a;//声明了一个数组指针 理解方便 int (*q) [3] = a; int (*q) [4] = a; 一次跳4个位子
printf("%p %p\n",a,a+1);
printf("%p %p\n\n",q,q+1);//返回值相同
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
printf("%p->%d\n",&a[i][j],a[i][j]);
printf("%p->%d\n",*(a+i)+j,*(*(a+i)+j));
}
printf("\n");
}
exit(0);
}
指针与字符数组
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *str = "hello";
printf("%d %d\n",sizeof(str),strlen(str));//8 5
//(F) strcpy(str,"world"); 此时动作为 world想要去覆盖str指向的常量
str = "world";//指针改变指向
puts(str);//hello
/*
char str[] = "hello";
printf("%d %d\n",sizeof(str),strlen(str));//6 5区别
#if 0
char str[] = "outputs";
//F str = "inputs";//数组名是地址常量
strcpy(str,"inputs");
puts(str);
#endif
/*
char str[] = "hello world!";
char *p = str+7;
puts(str);
puts(p);
*/
exit(0);
}
const 与指针
指针常量
常量指针
#include<stdio.h>
#include<stdlib.h>
#define PI 3.14//符号常量
//const float pi = 3.14;
/*
const int a;
int const a;
const int *p;//常量指针 --指针的指向可以发生变化,但是指向值不能变化(记忆方法 const (后一个指针的加星)值)
int const *p;//常量指针
int *const p;//指针常量 --指针的指向不可以发生变化,但是指向值能变化
const int *const p;//既是指针常量又是常量指针
*/
int main()
{
int i =1;
int j = 100;
const int *const p = &i;
//(F) p = &j;
//(F) *p = 10;
printf("%d\n",*p);
#if 0
int i =1;
int j =100;
int * const p = &i;
//(T) *p = 10;
//(F) p = &j;
printf("%d\n",*p);
#endif
#if 0
int i =1;
int j =100;
const int *p = &i;
// (F) *p = 10;
// (T) i = 10;
// (T) p = &j;
printf("%d\n",*p);
#endif
#if 0
const float pi = 3.14;//const 修饰的值 必须直接初始化
//F pi = 3.1415926;
float *p = π
*p = 3.1415926;//warning 报错
printf("%f\n",pi);
#endif
exit(0);
}
指针数组[存储类型] 数据类型 * 数组名【长度】//数组里面的类型是指针
如int * arr[3]; --> TYPE NAME; --> int *[3] arr;
int main()
{
int i,k,j;
char *name[5] = {"follow me","basic","great","fortran","computer"};
char *tmp;
for(i =0;i < 5-1; i++)
{
k =i;
for(j= i+1 ;j < 5;j++)
{
if(strcmp(name[k],name[j]) > 0)
k = j;
}
if(k! = i)
{
tmp = name[i];
name[i] = name[k];
name[k] = tmp;
}
}
for(i =0;i<5; i++)
puts(name[i]);
exit(0);
}
多级指针 man pthread
//argc **argv