此笔记由个人整理
塞上苍鹰_fly
课程来自:尚观C语言
直接访问:直接通过地址值访问得到此地址存放的内容
间接访问:通过指针变量所存放的地址值访问的到此地址存放的内容
不管你的指针是什么类型,是几级指针,在相同的编译环境中,所占的内存空间都是一致的。
案例
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i = 1;
int *p = &i;
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);
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
int *p = NULL;//空指针
int *q;//野指针
exit(0);
}
在不确定类型时使用
任何类型的值都可以把值赋给空类型指针,空类型指针也可以将内容赋给任何类型的指针
void *q;
int *p;
int *p = 123;
int **q;
int *p = &i;
int **q = &p;//二级指针
#include "stdio.h"
#include "stdlib.h"
int main()
{
int a[6] = {5,1,7,3,8,3};
int y;
int *p = &a[1];
y = (*--p)++;//在p地址的基础上地址自减一个元素,所以y的值为a[0]=5
//在执行完取值的操作后,a[0]所存放的值自增1
printf("y = %d\n",y);
printf("a[0] = %d\n",a[0]);
exit(0);
}
a[i]:a[i] = *(a+i) = *(p+i) = p[i]
&a[i]:&a[i] = a+i = p+i = &p[i]
#include "stdio.h"
#include "stdlib.h"
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
for(i = 0;i < 3;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[3];
int i;
int *p = a;
for(i = 0;i < sizeof(a)/sizeof(*a);i++)
printf("%p-->%d\n",&a[i],a[i]);
for(i = 0;i < sizeof(a)/sizeof(*a);i++)
scanf("%d",p++);
//scanf("%d",&a[i]);
//scanf("%d",&p[i]);
p = a;//将自增完的地址重新赋值
for(i = 0;i < sizeof(a)/sizeof(*a);i++,p++)
printf("%p-->%d\n",p,*p);
printf("/n");
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
int a[2][3] = {1,2,3,4,5,6};
int i,j;
int *p;
p = &a[0][0];
printf("a --> %p\n",a);
printf("a+1 --> %p\n",a+1);//a+1:地址增加一个行元素
printf("p --> %p\n",p);
printf("p+1 --> %p\n",p+1);//p+1:地址增加一个元素
printf("\n");
for(i = 0;i < 6;i++)
printf("%d ",p[i]);
printf("\n");
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[] = "I love china!";
char *p = str+7;
puts(str);
puts(p);
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
char str1[] = "hello";
char *str2 = "hello";
printf("str1:%d %d\n",sizeof(str1),strlen(str1));
printf("str2:%d %d\n",sizeof(str2),strlen(str2));
strcpy(str1,"world");
str2 = "world";
puts(str1);
printf("\n");
puts(str2);
printf("\n");
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
const float pi = 3.14;
float *p = π
*p = 3.14159;
printf("%f",pi);
exit(0);
}
const int a;
int const a;
const int *p;
int const *p;//常量指针
int *const p;//指针常量
const int *const p;//即是常量指针又是指针常量
#define PI 3.14
2 * PI * r;
const float pi = 3.14;
2 * pi * r;
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i = 1;
int j = 100;
const int *p = &i;
printf("before-->%d\n",*p);
//i = 10;//可以修改
//*p = 10;//报错:向只读位置“*p”赋值
p = &j;
printf("after-->%d\n",*p);
}//值不能变但指向可以变
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i = 1;
int j = 100;
int *const p = &i;
printf("defore-->%d\n",*p);
i = 100;
printf("after-->%d\n",*p);
//p = &j;//报错:向只读位置“p”赋值
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
int i = 1;
int j = 100;
const int *const p = &i;
printf("before-->%d\n",*p);
*p = 10;//报错:向只读位置“*p”赋值
p = &j;//报错:向只读位置“p”赋值
}
int (*p)[3]; --> int[3] *p;
#include "stdio.h"
#include "stdlib.h"
int main()
{
int a[2][3] = {1,2,3,4,5,6};
int i,j;
int *p = *a;
int (*q)[3] = a;
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",*(q+i)+j,*(*(q+i)+j));
//printf("%p --> %d\n",&a[i][j],a[i][j]);
}
printf("\n");
}
printf("%p %p\n",a,a+1);
printf("%p %p\n",q,q+1);
exit(0);
}
int *arr[3]; --> int *[3] arr;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
int i,j,k;
char *tmp;
char *name[5] = {"Fllow me","Basic","Great","Fortran","Computer"};
for(i = 0;i < 4;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);
}