1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

string function exercise

Discussion in 'C' started by avi, Aug 28, 2008.

  1. avi

    avi New Member

    Joined:
    Aug 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hi
    I written a very simlpe function in c and a main program to go with it:
    Code:
    void ToUpper(char * S)
    {
    	while (*S!='\0')
    	{
    		if (*S >= 'a' && *S <= 'z')
    		{
    			*S +='A'+ -'a';
    		}
    		S++;
    	}
    } 
    
    
    
    int main()
    {
    	char * sentence = "acbMNPtuv";
    	printf("%s\n",sentence);
    
    	ToUpper(sentence);
    
    	printf("after function: %s\n",sentence);
    	return 0;
    }
    
    
    it compiles but when i run it the comiler exits because of "0xC0000005: Access Violation".
    i debugged it and there seems to be a problem with:
    *S +='A'+ -'a';
    why?? i'm not drefrencing a NULL pointer so what the hell could be the problem??

    thanks
     
    Last edited by a moderator: Aug 28, 2008
  2. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    u are getting the error because string init at declaration is treated as constant and is stored in read only segment .So when you are trying to write the read only memory using pointer u will get this kind of access violation error :)
     
  3. avi

    avi New Member

    Joined:
    Aug 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi thanks for your reply.
    My question is how to change it so the string decleration is not considered a constant (i.e. not in the read only memory) do i have to configure something in my copiler?
    this all seemes very strange cause ive never encountered such problem and i didnt chane any settings...
    thanks again
     
  4. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    all pointer initialized data is treated as constant and is stored in read only segment!!!..this is c convention and not a problem with compiler setting!!!!


    so if you wanna correct this kind of problem don't init pointers at declaration ,use other methods :)
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Code:
    char sentence[32];
    strcpy(sentence, "acbMNPtuv");
    
     
  6. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    if u are still wondering why its like this
    a pointer is address to some location. so wen u initalize a pointer directly with some data that data is obviously a constant as its not stored in any variably previously.

    you may use any other variable kind of initialization for your string.i.e
    char sentence[]="acbMNPtuv";
    will also do....:)
     
  7. avi

    avi New Member

    Joined:
    Aug 28, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thanks alot people!!! you helped me very much
     

Share This Page