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

pointer string

Discussion in 'C' started by cimon, Oct 16, 2008.

  1. cimon

    cimon New Member

    Joined:
    Feb 9, 2007
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hello

    If I take a string declaration as below:

    char str[5]="Mike";
    than the following statement does NOT produce error
    str[start] ^= str[end];

    If I take a string declaration as below:
    char *str="Mike";
    that the above mentioned statement str[start] ^= str[end]; DOES produce error.

    So I want to know , that if my string declaration is as in second case (char *str="Mike";) than , how to write XOR statement so that it should not produce error.

    Thank You
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    The second case is a pointer to a string CONSTANT.
    So you cannot (generally) change it.
     
  3. cimon

    cimon New Member

    Joined:
    Feb 9, 2007
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Thank you
     
  4. micsom

    micsom New Member

    Joined:
    Oct 13, 2008
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    I*N*D*I*A*
    buddy i tried out ur code
    char *str="MIKE";
    str[0]^=str[4];
    printf(" %s",str);

    it gives no error... so i dont get it were is ur problem?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    micsom, you're probably using a different compiler, which one is it? Modifying a string constant should throw an error, since it's an operation you shouldn't perform.

    XOR-ing something with zero DOESN'T modify it, so mabye the compiler's outsmarting the lot of us. Try this:
    Code:
    char *str1="Mike";
    char *str2=str1;
    xtr1[0]^=str[3];
    printf("%s\n",str2);
    
     
  6. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    I have tried it in my pc....I modified it a bit....and it is running without error....

    my code is...
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    char *str1="Mike";
    char *str2=str1;
    str1[0]^=str1[4];
    printf("%s\n",str2);
    getch();
    }
    
    Is there anything wrong in my understanding??
     
  7. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Actually i first tried with th given code for which the output was:
    Code:
    (ike
    
    I don't know why....

    But then when I changed str1[3] to str1[4], the problem got solved immediately....cna anyone explain me why??
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well as I said you shouldn't modify string constants so str1[<anything>]^=<anthing> should throw an error - a compile time error if nothing else.
    However if it works, to find out why 'M' xor 'e' produces '(' you'll need to look at an ASCII table and the binary values of those three characters.
    'M' xor str1[4] = 'M', since str1[4]=0 (it's the terminating NULL). And as I said above, xor-ing anything with zero has no effect.
     
  9. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Thanks......now I hope I understand the lot....thank you very very much xpi0tos
     

Share This Page