首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++字符数组和字符串不是一样么,为什么函数调用会出错?该如何解决

2012-04-12 
C++字符数组和字符串不是一样么,为什么函数调用会出错?// task7.11_8.cpp : 定义控制台应用程序的入口点。/

C++字符数组和字符串不是一样么,为什么函数调用会出错?
// task7.11_8.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;
int replace(char* str,char c1,char c2);
int _tmain(int argc, _TCHAR* argv[])
{
char str[] = "windowssystem";
//char* str = "windowssystem"; //报错 task7.11_8.exe 中的 0x004115d3 处未处理的异常: 0xC0000005: 写入位置 0x00417816 时发生访问冲突 这是为什么???
char c1 = 's' ,c2 = '$';
int times = replace(str,c1,c2);

cout<<"返回替换次数:"<<times<<endl;
getchar();
return 0;
}

int replace(char* str,char c1,char c2)
{
int times = 0;
while (*str)
{
if (c1 == *str)
{
(*str) = c2;
times++;
}
str++;
}
  return times;
}



[解决办法]
char* str = "windowssystem"; 是字符串常量,不能修改其中的内容。
懂了的话记得结业给分哦

热点排行