关于《程序员编程宝典》中编写一个函数,作用是把一个char组成的字符串循环右移n位的问题

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include <string.h>

void LoopMove(char *pStr, int steps)
{
	int n = strlen(pStr) - steps;
	char temp[200];

	strcpy(temp, pStr+n);
	strcpy(temp+steps,pStr);
	*(temp + strlen(pStr)) = '\0';
	strcpy(pStr,temp);
	 
}

int main(int argc, char** argv[])
{
	//char ppstr[10] = "123456789";
	char* ppstr = "123456789";

	LoopMove(ppstr, 3);

	printf("%s", ppstr);

	system("pause");

	return 0;
}
这里如果把ppstr声明为数组类型,则执行没有错误,如果把ppstr声明为指针类型,则执行到strcpy(pStr,temp) 这句话时会出现中断,那么为什么会出现这种情况呢?
</pre><pre class="cpp" name="code">1,这这道题不仅是考察strcpy,strlen函数的使用,还考察了,字符串指针和字符数组的区别
2,区别: char ppstr[10] = "123456789";   //"123456789"存储在栈中,值可以被修改
 char* ppstr = "123456789";;      //"123456789"存储在静态常量区,ppstr 指向"123456789",即是ppstr保存的是"123456789"在常量区的地址,其值不能被修改。
所以,当声明字符串指针并初始化后,在函数LoopMove中使用strcpy(pStr,temp) 会改变ppstr的内容,所以会报错。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值