C语言 指针自增自减&加减运算 p++ p+i

Posted DQ_CODING

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 指针自增自减&加减运算 p++ p+i相关的知识,希望对你有一定的参考价值。

介绍

自增自减代码

#include<stdio.h>
#include<string.h>
//指针自增--short
void increase(short *arr,int len)
{
	int i;
	arr=&arr[0];
	for(i=0;i<len;i++)
	{
		printf("arr[%d]=%d,address=%p\\n",i,*arr,arr);
		arr++;
	}
}

//指针自减--char
void decrease(char *ptr,int len1)
{
	int i;
	printf("%s\\n",ptr);
	ptr=&ptr[len1-1];
	for(i=len1-1;i>=0;i--)
	{
		printf("ptr[%d]=%c,address=%p\\n",i,*ptr,ptr);
		ptr--;
	}
}
//整型自减--int
void down(int *arr,int len)
{
	int i;
	arr=&arr[len-1];
	for(i=len-1;i>=0;i--)
	{
		printf("arr[%d]=%d,address=%p\\n",i,*arr,arr);
		arr--;
	}
}
//字符自增--char
void up(char *s,int len)
{
	printf("s=%s\\n",s);
	s=&s[0];
	int i;
	for(i=0;i<len;i++)
	{
		printf("s[%d]=%c,address=%p\\n",i,*s,s);
		s++;
	}
}
int main()
{
	short arr[]={1,2,3,7,99};
	char ptr[]={'>','p','I','0','\\0'};//只有""后面才会默认加\\0
	char ptr1[]="hello dq";
	char *ptr2="www.com.pp";
	int arr1[]={888,9064,3,-3,-4};


	int len=sizeof(arr)/sizeof(short);
	int len1=strlen(ptr);
	int len2=sizeof(arr1)/sizeof(int);
	int len3=strlen(ptr1);
	int len4=strlen(ptr2);
	printf("short指针自增:\\n");
	increase(arr,len);
	printf("ptr的自增:\\n");
	up(ptr,len1);
	printf("ptr1的自增:\\n");
	up(ptr1,len3);
	printf("ptr2的自增:\\n");
	up(ptr2,len4);
	printf("int指针自减:\\n");
	down(arr1,len2);
	printf("char指针自减:\\n");
	decrease(ptr,len1);
	printf("char ptr1的自减\\n");
	decrease(ptr1,len3);
	printf("char ptr2的自减\\n");
	decrease(ptr2,len4);

	printf(" 测试1:\\n");
	printf(" ptr1的地址:%p\\n ptr1指向的内容%s\\n *ptr1的内容:%c\\n ",&ptr1,ptr1,*ptr1);
	printf(" 测试2:\\n");
	printf(" ptr2本身的地址:%p\\n ptr2指向的地址%p\\n ptr2指向的内容%s\\n *ptr2的内容:%c\\n ",&ptr2,ptr2,ptr2,*ptr2);
	getchar();
	return 0;
}



输出:

//输出:
//short指针自增:
//arr[0]=1,address=012FFBB4
//arr[1]=2,address=012FFBB6
//arr[2]=3,address=012FFBB8
//arr[3]=7,address=012FFBBA
//arr[4]=99,address=012FFBBC
//ptr的自增:
//s=>pI0
//s[0]=>,address=012FFBA4
//s[1]=p,address=012FFBA5
//s[2]=I,address=012FFBA6
//s[3]=0,address=012FFBA7
//ptr1的自增:
//s=hello dq
//s[0]=h,address=012FFB90
//s[1]=e,address=012FFB91
//s[2]=l,address=012FFB92
//s[3]=l,address=012FFB93
//s[4]=o,address=012FFB94
//s[5]= ,address=012FFB95
//s[6]=d,address=012FFB96
//s[7]=q,address=012FFB97
//ptr2的自增:
//s=www.com.pp
//s[0]=w,address=00D358E0
//s[1]=w,address=00D358E1
//s[2]=w,address=00D358E2
//s[3]=.,address=00D358E3
//s[4]=c,address=00D358E4
//s[5]=o,address=00D358E5
//s[6]=m,address=00D358E6
//s[7]=.,address=00D358E7
//s[8]=p,address=00D358E8
//s[9]=p,address=00D358E9
//int指针自减:
//arr[4]=-4,address=012FFB78
//arr[3]=-3,address=012FFB74
//arr[2]=3,address=012FFB70
//arr[1]=9064,address=012FFB6C
//arr[0]=888,address=012FFB68
//char指针自减:
//>pI0
//ptr[3]=0,address=012FFBA7
//ptr[2]=I,address=012FFBA6
//ptr[1]=p,address=012FFBA5
//ptr[0]=>,address=012FFBA4
//char ptr1的自减
//hello dq
//ptr[7]=q,address=012FFB97
//ptr[6]=d,address=012FFB96
//ptr[5]= ,address=012FFB95
//ptr[4]=o,address=012FFB94
//ptr[3]=l,address=012FFB93
//ptr[2]=l,address=012FFB92
//ptr[1]=e,address=012FFB91
//ptr[0]=h,address=012FFB90
//char ptr2的自减
//www.com.pp
//ptr[9]=p,address=00D358E9
//ptr[8]=p,address=00D358E8
//ptr[7]=.,address=00D358E7
//ptr[6]=m,address=00D358E6
//ptr[5]=o,address=00D358E5
//ptr[4]=c,address=00D358E4
//ptr[3]=.,address=00D358E3
//ptr[2]=w,address=00D358E2
//ptr[1]=w,address=00D358E1
//ptr[0]=w,address=00D358E0
// 测试1:
// ptr1的地址:012FFB90
// ptr1指向的内容hello dq
// *ptr1的内容:h
//  测试2:
// ptr2本身的地址:012FFB84
// ptr2指向的地址00D358E0
// ptr2指向的内容www.com.pp
// *ptr2的内容:w

加减代码

指针加减函数:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//指针加减函数
void move(char *s,int len,int m)
{

	int i;
	if(m>=0)//右移
	{                        
		printf("从前往后移\\n");
		printf("现在处于第%d位:%c,address=%p\\n",m,s[m],&s[m]);
		s+=m;
		for(i=m;i<len;i++)
		{
			printf("s[%d]=%c\\taddress=%p\\n",i,*s,s);
			s++;
		}
	}else//左移
	{
		printf("从后往前移\\n");
		//m<0
		printf("现在处于第%d位:%c,address=%p\\n",len+m-1,s[len+m-1],&s[len+m-1]);
		//s=&s[len+m-1]; 效果一样
	    s+=len+m-1;
		for(i=len+m-1;i>=0;i--)
		{
			printf("s[%d]=%c\\taddress=%p\\n",i,*s,s);
			s--;
		}

	}
}
int main()
{
	char *s="abcdefghijklmn";
	printf("s=%s\\n",s);
	printf("右移:\\n");
	move(s,strlen(s),2);
	printf("左移:\\n");
	move(s,strlen(s),-4);
	getchar();
	return 0;
}

输出:

s=abcdefghijklmn
右移:
从前往后移
现在处于第2:c,address=005857B6
s[2]=c  address=005857B6
s[3]=d  address=005857B7
s[4]=e  address=005857B8
s[5]=f  address=005857B9
s[6]=g  address=005857BA
s[7]=h  address=005857BB
s[8]=i  address=005857BC
s[9]=j  address=005857BD
s[10]=k address=005857BE
s[11]=l address=005857BF
s[12]=m address=005857C0
s[13]=n address=005857C1
左移:
从后往前移
现在处于第9:j,address=005857BD
s[9]=j  address=005857BD
s[8]=i  address=005857BC
s[7]=h  address=005857BB
s[6]=g  address=005857BA
s[5]=f  address=005857B9
s[4]=e  address=005857B8
s[3]=d  address=005857B7
s[2]=c  address=005857B6
s[1]=b  address=005857B5
s[0]=a  address=005857B4

以上是关于C语言 指针自增自减&加减运算 p++ p+i的主要内容,如果未能解决你的问题,请参考以下文章

c语言中条件判断表达式中出现自增自减符时的运算顺序

c语言中自增自减运算符的运算次序?

指针取值(*)与自增(++)运算

c语言中自增自减运算符的运算次序?

JS中的自增自减问题

C++指针的算术运算 关系运算