"pointer to char" address restoring problem

Discussion in 'C Programming' started by Mahesh, Feb 21, 2008.

  1. Mahesh

    Mahesh Guest

    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    int main(void)
    {
    int key = 120;
    char *s = "String to Encrypt using XOR";
    char *ss = s;
    char echar;
    char dchar;
    size_t sizeofmsg = strlen(s)*sizeof(char*);

    char *emsg = (char*)malloc(sizeofmsg);
    char *dmsg = (char*)malloc(sizeofmsg);

    char *ddmsg = dmsg;
    char *eemsg = emsg;
    int lenofmsg = strlen(s);
    int count=0;
    int i=0;

    while(count++<strlen(s)){

    echar = (char)(*ss^key);
    emsg = &echar;
    emsg++;
    ss++;
    }
    count=0;
    emsg = eemsg; // HERE when i restore the address of emsg via
    eemsg, i'm not able to
    // decrypt. How to restore the address of
    original pointer to char ?.
    i=0;
    printf("\n");
    while(count++<strlen(s)){

    echar = *emsg;
    dchar = (char)(echar ^ key);
    ddmsg = &dchar;
    printf(" %c",dchar);
    emsg++;
    ddmsg++;
    }
    printf("\n");
    getch();
    return 0;
    }
     
    Mahesh, Feb 21, 2008
    #1
    1. Advertising

  2. Mahesh

    Michael Mair Guest

    Mahesh wrote:
    > #include <stdio.h>
    > #include <string.h>
    > #include <malloc.h>


    Not a standard C header; use <stdlib.h> for malloc(), realloc(),
    free().

    > int main(void)
    > {
    > int key = 120;
    > char *s = "String to Encrypt using XOR";
    > char *ss = s;
    > char echar;
    > char dchar;
    > size_t sizeofmsg = strlen(s)*sizeof(char*);


    Wrong calculation.
    It should be (number of elements)*(size of the type s points to).
    That is,
    sizeofmsg = (strlen(s) + 1) * sizeof *s;
    as s points to char and sizeof (char) is guaranteed to be 1,
    you can write
    sizeofmsg = strlen(s) + 1;
    The "+1" stems from the string terminator that is not included
    in the string length but contributes to the string's size.

    >
    > char *emsg = (char*)malloc(sizeofmsg);
    > char *dmsg = (char*)malloc(sizeofmsg);


    Note that the cast is unnecessary and can hide an error; around
    here,
    T *p = malloc(NumberOfElems * sizeof *p);
    is recommended.

    > char *ddmsg = dmsg;
    > char *eemsg = emsg;
    > int lenofmsg = strlen(s);
    > int count=0;
    > int i=0;
    >
    > while(count++<strlen(s)){
    >
    > echar = (char)(*ss^key);


    Note that XOR encryption of the value key leads to 0 ('\0'),
    which also is the string terminator.

    > emsg = &echar;


    You probably mean
    *emsg = echar;

    &echar is just that, the address of echar; you do not change
    the contents of the storage area you got from malloc().
    So, in every iteration, you point emsg at the same object.

    Note that for s, you iterate on the copy, and for emsg, you
    iterate on the "original". This can lead to problems.

    > emsg++;
    > ss++;
    > }
    > count=0;
    > emsg = eemsg; // HERE when i restore the address of emsg via
    > eemsg, i'm not able to
    > // decrypt. How to restore the address of
    > original pointer to char ?.


    // style comments break easily on usenet; I recommend that you use
    /**/ comments which cannot change semantics or "compilability" due
    to line breaks

    > i=0;
    > printf("\n");
    > while(count++<strlen(s)){
    >
    > echar = *emsg;
    > dchar = (char)(echar ^ key);
    > ddmsg = &dchar;
    > printf(" %c",dchar);
    > emsg++;
    > ddmsg++;
    > }
    > printf("\n");
    > getch();
    > return 0;
    > }


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Feb 21, 2008
    #2
    1. Advertising

  3. Mahesh

    Mahesh Guest

    On Feb 21, 12:37 pm, Michael Mair <>
    wrote:
    > Mahesh wrote:
    > > #include <stdio.h>
    > > #include <string.h>
    > > #include <malloc.h>

    >
    > Not a standard C header; use <stdlib.h> for malloc(), realloc(),
    > free().
    >
    > > int main(void)
    > > {
    > >     int key = 120;
    > >     char *s = "String to Encrypt using XOR";
    > >     char *ss = s;
    > >     char echar;
    > >     char dchar;
    > >     size_t sizeofmsg = strlen(s)*sizeof(char*);

    >
    > Wrong calculation.
    > It should be (number of elements)*(size of the type s points to).
    > That is,
    >   sizeofmsg = (strlen(s) + 1) * sizeof *s;
    > as s points to char and sizeof (char) is guaranteed to be 1,
    > you can write
    >   sizeofmsg = strlen(s) + 1;
    > The "+1" stems from the string terminator that is not included
    > in the string length but contributes to the string's size.
    >
    >
    >
    > >     char *emsg = (char*)malloc(sizeofmsg);
    > >     char *dmsg = (char*)malloc(sizeofmsg);

    >
    > Note that the cast is unnecessary and can hide an error; around
    > here,
    >   T *p = malloc(NumberOfElems * sizeof *p);
    > is recommended.
    >
    > >     char *ddmsg = dmsg;
    > >     char *eemsg = emsg;
    > >     int lenofmsg = strlen(s);
    > >     int count=0;
    > >     int i=0;

    >
    > >     while(count++<strlen(s)){

    >
    > >         echar = (char)(*ss^key);

    >
    > Note that XOR encryption of the value key leads to 0 ('\0'),
    > which also is the string terminator.
    >
    > >         emsg = &echar;

    >
    > You probably mean
    >   *emsg = echar;
    >
    > &echar is just that, the address of echar; you do not change
    > the contents of the storage area you got from malloc().
    > So, in every iteration, you point emsg at the same object.
    >
    > Note that for s, you iterate on the copy, and for emsg, you
    > iterate on the "original". This can lead to problems.
    >
    > >         emsg++;
    > >         ss++;
    > >     }
    > >     count=0;
    > >     emsg = eemsg;  // HERE when i restore the address of emsg via
    > > eemsg, i'm not able to
    > >                             // decrypt. How to restore the address of
    > > original pointer to char ?.

    >
    > // style comments break easily on usenet; I recommend that you use
    > /**/ comments which cannot change semantics or "compilability" due
    > to line breaks
    >
    > >     i=0;
    > >     printf("\n");
    > >     while(count++<strlen(s)){

    >
    > >         echar = *emsg;
    > >         dchar = (char)(echar ^ key);
    > >         ddmsg = &dchar;
    > >         printf(" %c",dchar);
    > >         emsg++;
    > >         ddmsg++;
    > >     }
    > >     printf("\n");
    > >     getch();
    > >     return 0;
    > > }

    >
    > Cheers
    >   Michael
    > --
    > E-Mail: Mine is an   /at/ gmx /dot/ de   address.


    Thanks you so much Michael for you invaluable reply.

    -
    Mahesh
     
    Mahesh, Feb 21, 2008
    #3
  4. Mahesh

    pete Guest

    Mahesh wrote:
    >
    > On Feb 21, 12:37 pm, Michael Mair <>
    > wrote:
    > > Mahesh wrote:
    > > > #include <stdio.h>
    > > > #include <string.h>
    > > > #include <malloc.h>

    > >
    > > Not a standard C header; use <stdlib.h> for malloc(), realloc(),
    > > free().
    > >
    > > > int main(void)
    > > > {
    > > > int key = 120;
    > > > char *s = "String to Encrypt using XOR";
    > > > char *ss = s;
    > > > char echar;
    > > > char dchar;
    > > > size_t sizeofmsg = strlen(s)*sizeof(char*);

    > >
    > > Wrong calculation.
    > > It should be (number of elements)*(size of the type s points to).
    > > That is,
    > > sizeofmsg = (strlen(s) + 1) * sizeof *s;
    > > as s points to char and sizeof (char) is guaranteed to be 1,
    > > you can write
    > > sizeofmsg = strlen(s) + 1;
    > > The "+1" stems from the string terminator that is not included
    > > in the string length but contributes to the string's size.


    These two idioms are both worth knowing:

    ptr = malloc(NMEMB * sizeof *ptr);

    ptr = malloc(STRING_LENGTH + 1);

    --
    pete
     
    pete, Feb 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhishek

    Casting a file pointer to char pointer

    Abhishek, Mar 21, 2006, in forum: C Programming
    Replies:
    9
    Views:
    668
    santosh
    Mar 22, 2006
  2. Jack

    pointer-to-pointer to char

    Jack, May 28, 2006, in forum: C Programming
    Replies:
    22
    Views:
    829
    Andrew Poelstra
    May 30, 2006
  3. arnuld
    Replies:
    13
    Views:
    659
  4. babu
    Replies:
    0
    Views:
    288
  5. Xavier Roche
    Replies:
    3
    Views:
    151
    James Kuyper
    Mar 25, 2014
Loading...

Share This Page