c++ - 将字符串 Upper char 更改为 Lower char 的程序出错

标签 c++ string visual-studio-2010

调试时我总是收到错误信息,显示为“Utou.exe 中 0x004113ea 处未处理的异常:0xC0000005:访问冲突写入位置 0x00415835。”在行 "*s -= 32; " 谁能帮忙解决问题?

#include "stdafx.h"
#include <iostream>
using namespace std;
void ToUpperPtr(char* s)
{
    //char *a;
    //a=s;
    while(*s != '\0')
    {

        if(*s >='a' && *s <='z')
            *s -= 32;      
        s++; // 
    }

    //printf("%s",a);
}

int _tmain(int argc, _TCHAR* argv[])
{

    char *a="AbcdfrDFD";

    ToUpperPtr(a);
    //printf("%s",a);
    int i;
    scanf("%d",&i);
    return 0;
}

最佳答案

简单来说,您正在尝试更改会导致访问冲突的文字。

char *a="AbcdfrDFD";

试试这个..

char a[]="AbcdfrDFD";

关于c++ - 将字符串 Upper char 更改为 Lower char 的程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781188/

相关文章:

visual-studio-2010 - 无法打开VSVim项目

visual-studio-2010 - 将字段集合传递到汇编方法中

visual-studio - 在五个以上的内核上并行运行单元测试

linux - 替换zsh中字符串的一部分

java - 性能 (JAVA) ~ 循环中的字符串连接,带有前置和附加

c++ - 添加 cpp 文件后体系结构 x86_64 的 undefined symbol

php - 我如何制作在线编译和运行C++程序的网站

c++ - 如何在内存中保留 C++ 对象?

c++ - realloc() 的性能消耗

c - 分析字符串的一部分