0

I always get error info when debuging which shows like "Unhandled exception at 0x004113ea in Utou.exe: 0xC0000005: Access violation writing location 0x00415835." at line " *s -= 32; " who can help what's the problem?

#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;
}
user1279988
  • 757
  • 1
  • 11
  • 27

1 Answers1

3

In simple terms, you are trying to change a literal which gives you access violation.

char *a="AbcdfrDFD";

Instead try this..

char a[]="AbcdfrDFD";
vidit
  • 6,293
  • 3
  • 32
  • 50
  • @user1279988: the pointer declaration means that you're actually modifying a string literal (which is read-only). see this for more info: http://stackoverflow.com/questions/480555/modifying-c-string-constants – Ilian Mar 20 '12 at 04:17
  • @user1279988- `char a[]` allocates memory and then initializes, whereas `char *a` creates a pointer to a string literal. – vidit Mar 20 '12 at 04:18
  • @user1279988: please don't forget to accept vidit's answer. you can accept by clicking the check mark. – Ilian Mar 20 '12 at 04:25