以下代码是VC++程序员大全的范例代码:
#define ELBA "Able was I ere I saw Elba" char *pszElba = ELBA;
char ch;
size_t x = 0;
size_t max = strlen(ELBA);
printf("%s\n", pszElba);
while (x < max / 2)
{
ch = pszElba[x];
pszElba[x] = pszElba[max - x - 1]; //运行到这里就出现非法操作?
pszElba[max - x - 1] = ch;
++x;
}
printf("%s\n", pszElba);
return (0);
#define ELBA "Able was I ere I saw Elba" char *pszElba = ELBA;
char ch;
size_t x = 0;
size_t max = strlen(ELBA);
printf("%s\n", pszElba);
while (x < max / 2)
{
ch = pszElba[x];
pszElba[x] = pszElba[max - x - 1]; //运行到这里就出现非法操作?
pszElba[max - x - 1] = ch;
++x;
}
printf("%s\n", pszElba);
return (0);
解决方案 »
- WH_CALLWNDPROC为什么可以可以把其他程序勾到自己的程序中
- MFC下使用CDataBase类打开数据库失败
- 求助CRT函数库
- 短短几句话,内存狂被吃,IHTMLElement接口编程求救!
- LIST BOX添加数据的问题?
- ListCtrl的LVN_ITEMCHANGED好像只支持方向键换行,如果用鼠标点击换行,当前行为换行前的行,不只大家发现没有?
- 请问CCJControlBar是个什么类??
- 有关于网络编程的实例下载吗??
- 怎样实现在程序的任何地方调用CMainFrame::OnCreate?也就是刷新它?
- 文件的读写操作,在线等待给分
- 高分悬赏,有胆量的进来拿分,准备1000分!
- 请问Install Shield如何使用?
因为系统默认以上语句为const类型,也就是系统默认其为一个常量字符串所以出现非法赋值,可以改为char pszElba[]=ELBA;
是不是上面那些代码在别的编译环境中可能也可以通过?我用的是vc++2005//用指针修改常量的值#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;const char *NETWORK = "A network can be loosely defined "\
"as the hardware and software that "\
"allow two entitles to communicate.";char *str1 = (char *)NETWORK;
char *str2 = (char *)NETWORK;
int main()
{
char *s = strstr(str1, " two");
if (s != NULL)
strcpy(s, " 2 entitles to communicate");
cout << str1 << endl;
cout << str2 << endl;
return (0);
}
char *str2 = (char *)NETWORK;NETWORK是一个const指针,所以强制转换会出错