指针的使用问题

今天在看剑指offer里面的28题,字符串的排列,把书上的程序敲到VC里面加个main函数老是运行有错误,具体程序如下:

#include<iostream>
using namespace std;

void Permutation(char *pstr,char *pbegin);

void Permutation(char *pstr)
{
	if(pstr==NULL)
		return;
	Permutation(pstr,pstr);
}

void Permutation(char *pstr,char *pbegin)
{
	if(*pbegin=='\0')
		cout<<pstr<<endl;
	else
	{
		for(char *pch=pbegin;*pch!='\0';pch++)
		{
			char tmp=*pch;
			*pch=*pbegin;
			*pbegin=tmp;
			
			Permutation(pstr,pbegin+1);
			
			tmp=*pch;
			*pch=*pbegin;
			*pbegin=tmp;
		}
	}
}

int main()
{
	char *str="abc";
	Permutation(str);
	return 0;
}
问题就出在这个main函数里面,char *pstr="abc",在网上查了一下,pstr是在栈上的,而"abc"是在常量区,静态存储区,是只读的不可写。

如果用char str[]="abc",就是可读,可写的;并且如果用char *p,然后p=malloc()来申请一块内存,也是可读可写的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值