指针的定义:
指针是变量的一种,里面是一个地址,通过这个地址找到要找的数据。
格式:如int *a short *a char *a....
含义: int a; int *p=&a(也可以写成int *p p=&a);指定P指向以变量a命名的内存空间
赋值 a=100;通过变量名访问空间
*p=100通过地址访问P所指向内存空间,并修改那个空间的值
指针类型大小:不管是int *还是char *,long *都是4个字节;sizeof(int*)
注意:char*p=&a;
p+1 指向地址加一个字节(0x100 到0x101)
int*p =&a; p+1指向地址向后移动4个字节(0x100到0x104)
程序:将a和b的值互换
#include <stdio.h>
void swap(int *x ,int *y)//函数调用后形参的值会被清空,在没有返回值的情况就失去意义
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int main()
{
int a=1,b=3;
swap(&a,&b);//所以要想通过形参改变实参的值需要传递地址,通过地址找到a和b的内存空间改变其值;
printf("%d%d\n",a,b);//虽然函数调用形参x 和y被释放但其实只是释放指针x和y里面的地址,修改的值被保存下来
}
计算指针:
#include <stdio.h>
int main()
{
int x=0,j=0;
int *px=&x;
printf("%p\n",px);
j=*px+3;
printf("%d\n",j);.//通过指针p访问x这段内存空间*PX取x里面的值+3
j=++*px ;//先取值后加1
printf("%d\n",j);
j=*px++ ;//(优先计算px++,int类型地址加4个字节)
printf("%p\n",px);
}
结果:
0xbfb22258
3
1
0xbfb2225c
问题:用指针实现strcpy函数
注意strcpy连同‘\0'一起复制
#include <stdio.h>
char * mystrcopy(char *x, char *y)
{
//char *tmp=x;//指针等于指针,使tmp指针和x指向同一块内存
while((*x++=*y++) !='\0');//(++)指针移动并取(*)所指内存空间的值;注意*比++优先级一样按顺序执行
return tmp;//当while()里面等于'\0'时循环结束
}
int main()
{
char str[20]="helloworld";
char ptr[20]="woo";
char temp[20];
mystrcopy(str ,ptr);//传字符数组首元素的地址
printf("%s\n",str);
}
输出:woo
空指针和野指针:
#define NULL 0 int *p=NULL; 空指针 p为指针变量,p=NULL表示空指针,不指向任何变量。
单独int *p为野指针他们都不能使用会报错;
申请堆空间:格式(强制转换类型)malloc(sizeof(申请空间大小)*20)
#include <stdio.h>
#include<string.h>
#include <stdlib.h>//malloc必要的投文件
int main()
{
char*fp;
fp=(char *)malloc(sizeof(char)*20);
if(NULL == fp)
{
printf("malloc falure\n");//如果申请不到打印报错
}
strcpy(fp,"hello");// char *fp="hello ";fp指向有hello字符串的内存空间
printf("%s\n",fp);//通过地址找到hello打印出来,printf scanf都可以通过 指针,数组,变量 的地址访问所对应的值并输出和输入;
free(fp);//释放malloc堆空间
}
打印数组值的两种方法
int a[10];
int *p =a;
for(i=0;i<10;i++)
{
printf("%d\n",*(a+i));指针法
printf("%d"*(p+i));
或者 char*q="hello world"
for(i0;i<20;I++)
{
printf("%c",q[i]);
}
程序:
#include <stdio.h>
int main()
{
int i;
int a[10]={1,2,3,4,5};
int*p=a;
for(i=0;i<10;i++)
{
// printf("%d",a[i]);
// printf("%d",*(a+i));
printf("%d",*(p+i));
}
printf("\n");
char *q="hellwoworld";
for(i=0;i<10;i++)
{
printf("%c",q[i]);
}
printf("\n");
return 0;
题目:判断输入字符串是不是子串,使用strncmp函数比较
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i,len1,len2;
char *str =(char*)malloc(sizeof(char)*50);//分配50个char类型字节
char *ptr=(char*)malloc(sizeof(char)*50);
printf("please input string\n");
scanf("%s%s",str,ptr);//让指针指向所输入的字符串
len1=strlen(str);//通过指针访问的地址计算字符串长度
len2=strlen(ptr);
for(i=0;i<len1-len2+1;i++)//通过分析2个字符串长度与比较次数关系
{
if(strncmp(str + i,ptr,len2)==0)//指针指向的元素随着i变化后移,2个字符串通过str ptr指针比较,比较长度为len2
{//等于0,2个字符串在该长度 相等
printf("the %s is %s zichaun\n",ptr,str);
break;//采集到就跳出for循环
}
if(i==len1-len2)//如果i加到循环次数上限说明没有子串
{
printf("%s is not %s\n",ptr,str);
}
}
}
题目:分析输出}
#include <stdio.h>
int main()
{
int a[5]={1,2,3,4,5};
int *p1=(int*)(&a+1);//加了20个字节输出垃圾值
int*p2=(int*)((int)a+1);//强制转换成int型意义不明
int *p3=(int*)(a+1);//首元素地址+1指针往后移一位
printf("%d\n",p1[0]);
printf("%d\n",p2[0]);
printf("%d\n",p3[0]);//指针当成数组使用指向字符串或者数组
}
题目:将字符串倒叙输出
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
void revers(char*x ,char*y)
{
int len = strlen(x),i;//通过地址检测字符串长度
x +=(len -1);//指针指向字符串最后一位
for(i=0;i<len;i++)//通过指针x取值给指针y所指内存
{
*y=*x;
y++;指针y往后移
x--;指针x往前移动达到倒叙输出作用
}
}
int main()
{
char * str= "helloworld";
printf("xxx\n");//检测段错误快捷方法
char *ptr=(char*) malloc (sizeof(char)*64);
printf("xxx\n");
revers(str,ptr);
printf("xxx\n");
printf("%s\n",ptr);
}
注意:如果指针变量p已指向数组中的一个元素,则p+1指向同一数组中的下一个元素。 例: P的初值为&a[0] : p+i和a+i都是a[i]的地址。 *(p+i)和*(a+i)都是p+i和a+i所指向的数组元素,即a[i].
指针数组
格式 数据类型* 数组名[长度]
#include <stdio.h>
int main()
{
char * str[100]={"i love china","me too","i see"};
printf("%s\n",str[0]);//输出字符串i love china
printf("%s\n",str[1]);//me too
printf("%s\n",str[2]);
return 0;
}//3个元素都是指针指向3个字符串
指针和字符串
字符串定义方法
char *ptr="hello world"定义指向字符串的指针
char b[20]="string";
strcpy(b,"string")
字符数组和字符串的区别:
1. 字符串指针变量本身是一个变量,用于存放字符串的首地址。而字符串本身是存放在以该首地址为首的一块连续的内存空间中并以‘\0’作为串的结束。
2. 对字符串指针方式 char *ps="C Language"; 可以写为: char *ps; ps="C Language";
而对数组方式: char st[]={"C Language"}; 不能写为: char st[20]; st={"C Language"};