Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. char * query [modified]

char * query [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasecomquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prasad_som
    wrote on last edited by
    #1

    I've small piece of code,My query is , why line in bold not work ?

    int main(int argc, char* argv[])
    {
    char *pp=new char[2];
    char c='I';
    char *str="test";
    strcpy(pp,"dynamic");
    pp[0]=c;//this works
    cout< str[0]=c; //why this doesn't work ?
    delete []pp;
    return 0;
    }

    -- modified at 6:56 Tuesday 12th September, 2006

    Prasad Notifier using ATL

    S D N Z 4 Replies Last reply
    0
    • P prasad_som

      I've small piece of code,My query is , why line in bold not work ?

      int main(int argc, char* argv[])
      {
      char *pp=new char[2];
      char c='I';
      char *str="test";
      strcpy(pp,"dynamic");
      pp[0]=c;//this works
      cout< str[0]=c; //why this doesn't work ?
      delete []pp;
      return 0;
      }

      -- modified at 6:56 Tuesday 12th September, 2006

      Prasad Notifier using ATL

      S Offline
      S Offline
      Sebastian Schneider
      wrote on last edited by
      #2

      Because you do not know where the compiler placed the string "test". What you did with that one was telling the compiler to give you the adress of "test". That means, the compiler has to find a location to place "test" in. And that might, following the C++-Standard, be anywhere, even locations that are not writeable from inside the code. You should not make any assumptions on where the compiler places this. You need to assign memory to the char*, then copy "test" into the char*, in order for this to work.

      Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

      P 1 Reply Last reply
      0
      • S Sebastian Schneider

        Because you do not know where the compiler placed the string "test". What you did with that one was telling the compiler to give you the adress of "test". That means, the compiler has to find a location to place "test" in. And that might, following the C++-Standard, be anywhere, even locations that are not writeable from inside the code. You should not make any assumptions on where the compiler places this. You need to assign memory to the char*, then copy "test" into the char*, in order for this to work.

        Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        Ok.Thanks. I was in assumption that,

        char* str="test";
        cout<
        is possible , then reverse is also possible.
        Thanks.

        Prasad
        Notifier using ATL

        1 Reply Last reply
        0
        • P prasad_som

          I've small piece of code,My query is , why line in bold not work ?

          int main(int argc, char* argv[])
          {
          char *pp=new char[2];
          char c='I';
          char *str="test";
          strcpy(pp,"dynamic");
          pp[0]=c;//this works
          cout< str[0]=c; //why this doesn't work ?
          delete []pp;
          return 0;
          }

          -- modified at 6:56 Tuesday 12th September, 2006

          Prasad Notifier using ATL

          D Offline
          D Offline
          Defenestration
          wrote on last edited by
          #4

          Because *str is a pointer to a string constant "test", and not an array of characters. The pointer *str can be made to point to a different location, but the contents of a string constant cannot be changed (the ANSI C Standard actually says trying to modify the contents of string constant is undefined - ie. sometimes it might work, other times it may not - so you should never be writing code like this) Writing the following would work because str would be an array of characters. The contents of the array could be changed, but str will always refer to the same storage. chat str[] = "test"; str[0] = c;

          P 1 Reply Last reply
          0
          • P prasad_som

            I've small piece of code,My query is , why line in bold not work ?

            int main(int argc, char* argv[])
            {
            char *pp=new char[2];
            char c='I';
            char *str="test";
            strcpy(pp,"dynamic");
            pp[0]=c;//this works
            cout< str[0]=c; //why this doesn't work ?
            delete []pp;
            return 0;
            }

            -- modified at 6:56 Tuesday 12th September, 2006

            Prasad Notifier using ATL

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            prasad_som wrote:

            char *str="test";

            prasad_som wrote:

            I've small piece of code,My query is , why line in bold not work ?

            Because compiler places these type of strings in read only memory area.


            Nibu thomas A Developer Programming tips[^]  My site[^]

            P 1 Reply Last reply
            0
            • D Defenestration

              Because *str is a pointer to a string constant "test", and not an array of characters. The pointer *str can be made to point to a different location, but the contents of a string constant cannot be changed (the ANSI C Standard actually says trying to modify the contents of string constant is undefined - ie. sometimes it might work, other times it may not - so you should never be writing code like this) Writing the following would work because str would be an array of characters. The contents of the array could be changed, but str will always refer to the same storage. chat str[] = "test"; str[0] = c;

              P Offline
              P Offline
              prasad_som
              wrote on last edited by
              #6

              Thanks for informatioin.

              Prasad Notifier using ATL

              1 Reply Last reply
              0
              • N Nibu babu thomas

                prasad_som wrote:

                char *str="test";

                prasad_som wrote:

                I've small piece of code,My query is , why line in bold not work ?

                Because compiler places these type of strings in read only memory area.


                Nibu thomas A Developer Programming tips[^]  My site[^]

                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #7

                thanks.

                Prasad Notifier using ATL

                1 Reply Last reply
                0
                • P prasad_som

                  I've small piece of code,My query is , why line in bold not work ?

                  int main(int argc, char* argv[])
                  {
                  char *pp=new char[2];
                  char c='I';
                  char *str="test";
                  strcpy(pp,"dynamic");
                  pp[0]=c;//this works
                  cout< str[0]=c; //why this doesn't work ?
                  delete []pp;
                  return 0;
                  }

                  -- modified at 6:56 Tuesday 12th September, 2006

                  Prasad Notifier using ATL

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #8

                  prasad_som wrote:

                  char *pp=new char[2]; ... strcpy(pp,"dynamic");

                  Slightly off topic ... the above code will corrupt memory. pp is a pointer to a character array that has allocated space for 2 characters. "dynamic" is longer than 2, so copying it into pp will overwrite memory you don't control.

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                  P 1 Reply Last reply
                  0
                  • Z Zac Howland

                    prasad_som wrote:

                    char *pp=new char[2]; ... strcpy(pp,"dynamic");

                    Slightly off topic ... the above code will corrupt memory. pp is a pointer to a character array that has allocated space for 2 characters. "dynamic" is longer than 2, so copying it into pp will overwrite memory you don't control.

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    P Offline
                    P Offline
                    prasad_som
                    wrote on last edited by
                    #9

                    thanks, for correcting me. I overlooked it, to say my point.:)

                    Prasad Notifier using ATL

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups