Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串上字符更改为下字符的程序出错_C++_String_Visual Studio 2010 - Fatal编程技术网

C++ 将字符串上字符更改为下字符的程序出错

C++ 将字符串上字符更改为下字符的程序出错,c++,string,visual-studio-2010,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') {

调试时,我总是会得到错误信息,如“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;
}
#包括“stdafx.h”
#包括
使用名称空间std;
void ToUpperPtr(字符*s)
{
//char*a;
//a=s;
而(*s!='\0')
{

如果用简单的术语(*s>='a'&&&*s,那么您正试图更改导致访问冲突的文本

char *a="AbcdfrDFD";
相反,试试这个

char a[]="AbcdfrDFD";

简单地说,您正在尝试更改一个文本,它会导致访问冲突

char *a="AbcdfrDFD";
相反,试试这个

char a[]="AbcdfrDFD";

@user1279988:指针声明意味着您实际上正在修改字符串文字(只读)。有关详细信息,请参阅此:@user1279988-
char a[]
分配内存,然后进行初始化,而
char*a
创建一个指向字符串文字的指针。@user1279988:请不要忘记接受vidit的答案。您可以通过单击复选标记来接受。@user1279988:指针声明意味着您实际上正在修改字符串文字(只读)。有关详细信息,请参见此:@user1279988-
char a[]
分配内存,然后进行初始化,而
char*a
创建指向字符串文字的指针。@user1279988:请不要忘记接受vidit的答案。单击复选标记可以接受。