How do I write this in pointer notation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angela

    How do I write this in pointer notation?

    //array notation works********** *
    char temp[]

    for(int x=0; x<strlen(temp) ; x++)
    { temp[x]=(temp[x]*2); } //simple char encoding

    //how do I do the same thing with pointer notation******* ***
    char* temp

    while( *temp )
    { *temp++= (*temp)*2; } //simple char encoding

    I have played around with this and usually it just erases the first
    few letters. If I wanted to do something like this with a C++ string,
    is this possible?
  • lilburne

    #2
    Re: How do I write this in pointer notation?

    Angela wrote:
    [color=blue]
    > //array notation works********** *
    > char temp[]
    >
    > for(int x=0; x<strlen(temp) ; x++)
    > { temp[x]=(temp[x]*2); } //simple char encoding
    >
    > //how do I do the same thing with pointer notation******* ***
    > char* temp
    >
    > while( *temp )
    > { *temp++= (*temp)*2; } //simple char encoding
    >
    > I have played around with this and usually it just erases the first
    > few letters. If I wanted to do something like this with a C++ string,
    > is this possible?[/color]


    { *temp++= (*temp)*2; } //simple char encoding

    The *temp++ has the side effect of incrementing temp, so the
    temp on the left is not the same as the temp on the right.

    *temp++ *= 2;


    Comment

    • Artie Gold

      #3
      Re: How do I write this in pointer notation?

      Angela wrote:[color=blue]
      > //array notation works********** *
      > char temp[]
      >
      > for(int x=0; x<strlen(temp) ; x++)
      > { temp[x]=(temp[x]*2); } //simple char encoding
      >
      > //how do I do the same thing with pointer notation******* ***
      > char* temp
      >
      > while( *temp )
      > { *temp++= (*temp)*2; } //simple char encoding[/color]

      You cannot do it this way; the value of `temp' is being altered
      without an intervening sequence point causing undefined behavior.

      One correct option would be to mimic the array form, as in:

      for (; *temp; temp++)
      *temp = *temp * 2;
      [color=blue]
      >
      > I have played around with this and usually it just erases the first
      > few letters.[/color]

      Yes, due to the invocation of undefined behavior noted above.
      [color=blue]
      > If I wanted to do something like this with a C++ string,
      > is this possible?[/color]

      Yes. Certainly. (Look up `iterator' and `transform', for example)

      HTH,
      --ag

      --
      Artie Gold -- Austin, Texas
      Oh, for the good old days of regular old SPAM.

      Comment

      • Artie Gold

        #4
        Re: How do I write this in pointer notation?

        lilburne wrote:

        [snip][color=blue]
        >
        >
        > { *temp++= (*temp)*2; } //simple char encoding
        >
        > The *temp++ has the side effect of incrementing temp, so the temp on the
        > left is not the same as the temp on the right.
        >
        > *temp++ *= 2;[/color]

        Better than my original reply! (on this point)
        [color=blue]
        >
        >[/color]
        --ag


        --
        Artie Gold -- Austin, Texas
        Oh, for the good old days of regular old SPAM.

        Comment

        Working...