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. A Simple C++ Question.

A Simple C++ Question.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comhelp
14 Posts 7 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.
  • S Sivaraman Dhamodharan

    Have a look at the below code:

    #include "stdafx.h"
    #include "conio.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    //Snippet 1
    char name[20] = "Some Name";
    printf("%s\n", name);
    name[1] = 'a';
    printf("%s\n", name);

    //Snippet 2
    char \* name2 = "Some Name";
    printf("%s\\n", name2);
    name2\[1\] = 'a';
    printf("%s\\n", name2);
    
    getch();
    return 0;
    

    }

    Why we get Runtime error when we modify name2[1] = 'a';?

    Programming Article

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    It all boils down to the difference between an array and a pointer. For name, you have set aside room for 20 characters (that are initialized to "Some Name"). For name2, you are pointing to a static piece of memory. That memory cannot be changed.

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

    S 1 Reply Last reply
    0
    • S Sivaraman Dhamodharan

      Have a look at the below code:

      #include "stdafx.h"
      #include "conio.h"

      int _tmain(int argc, _TCHAR* argv[])
      {
      //Snippet 1
      char name[20] = "Some Name";
      printf("%s\n", name);
      name[1] = 'a';
      printf("%s\n", name);

      //Snippet 2
      char \* name2 = "Some Name";
      printf("%s\\n", name2);
      name2\[1\] = 'a';
      printf("%s\\n", name2);
      
      getch();
      return 0;
      

      }

      Why we get Runtime error when we modify name2[1] = 'a';?

      Programming Article

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #3

      In this case... your definition of name2 is what is referred to as a string literal and points to data that is considered constant. See here[^].

      1 Reply Last reply
      0
      • S Sivaraman Dhamodharan

        Have a look at the below code:

        #include "stdafx.h"
        #include "conio.h"

        int _tmain(int argc, _TCHAR* argv[])
        {
        //Snippet 1
        char name[20] = "Some Name";
        printf("%s\n", name);
        name[1] = 'a';
        printf("%s\n", name);

        //Snippet 2
        char \* name2 = "Some Name";
        printf("%s\\n", name2);
        name2\[1\] = 'a';
        printf("%s\\n", name2);
        
        getch();
        return 0;
        

        }

        Why we get Runtime error when we modify name2[1] = 'a';?

        Programming Article

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #4

        You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).

        std::string name = "some name";
        std::cout << name << std::endl;

        std::string name2 = "some name";
        std::cout << name2 << std::endl;

        name2[1] = 'a'; // need to check that the index is within the string size.
        std::cout << name2 << std::endl;

        Watched code never compiles.

        C A 2 Replies Last reply
        0
        • M Maximilien

          You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).

          std::string name = "some name";
          std::cout << name << std::endl;

          std::string name2 = "some name";
          std::cout << name2 << std::endl;

          name2[1] = 'a'; // need to check that the index is within the string size.
          std::cout << name2 << std::endl;

          Watched code never compiles.

          C Offline
          C Offline
          Chuck OToole
          wrote on last edited by
          #5

          Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.

          J A 2 Replies Last reply
          0
          • M Maximilien

            You are trying to modify a const char array (pointer). This is not C++. if you were writing proper C++, you would not get those problems (or a lot less likely).

            std::string name = "some name";
            std::cout << name << std::endl;

            std::string name2 = "some name";
            std::cout << name2 << std::endl;

            name2[1] = 'a'; // need to check that the index is within the string size.
            std::cout << name2 << std::endl;

            Watched code never compiles.

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #6

            Agree with Chuck... technically it is C++.

            1 Reply Last reply
            0
            • C Chuck OToole

              Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #7

              Chuck O'Toole wrote:

              Technically, to pick nits, it *is* C++ as C++ is a superset of C

              Does the newest ANSI C++ standard incorporate the latest ANSI C standard as a subset? Last I heard that was not the case

              C 1 Reply Last reply
              0
              • C Chuck OToole

                Technically, to pick nits, it *is* C++ as C++ is a superset of C. What you are really saying is that it isn't "Object Oriented Programming" which is true, but it is still within the definition of C++.

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #8

                C++98 is not a superset of C90 - especially when you consider const and what happens when you cast away const. C allows modification of literals (string and constants) through pointers while C++ doesn't or rather considers it undefined behaviour.

                C 1 Reply Last reply
                0
                • J jschell

                  Chuck O'Toole wrote:

                  Technically, to pick nits, it *is* C++ as C++ is a superset of C

                  Does the newest ANSI C++ standard incorporate the latest ANSI C standard as a subset? Last I heard that was not the case

                  C Offline
                  C Offline
                  Chuck OToole
                  wrote on last edited by
                  #9

                  The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.

                  J 1 Reply Last reply
                  0
                  • A Aescleal

                    C++98 is not a superset of C90 - especially when you consider const and what happens when you cast away const. C allows modification of literals (string and constants) through pointers while C++ doesn't or rather considers it undefined behaviour.

                    C Offline
                    C Offline
                    Chuck OToole
                    wrote on last edited by
                    #10

                    The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.

                    1 Reply Last reply
                    0
                    • C Chuck OToole

                      The point is, C++ did *not* decommit the "char" data type nor pointers. std::string is *not* the only way to do string manipulation in C++. It may be the perferred way for some people but it's not the only way.

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #11

                      Chuck O'Toole wrote:

                      The point is, C++ did *not* decommit the "char" data type nor pointer

                      I was responding to what appeared to be a general comment about the language in general and in its entirety and not just one small part. And that general comment, at this time (new standards), is wrong.

                      C 1 Reply Last reply
                      0
                      • J jschell

                        Chuck O'Toole wrote:

                        The point is, C++ did *not* decommit the "char" data type nor pointer

                        I was responding to what appeared to be a general comment about the language in general and in its entirety and not just one small part. And that general comment, at this time (new standards), is wrong.

                        C Offline
                        C Offline
                        Chuck OToole
                        wrote on last edited by
                        #12

                        Context dude, gotta read comments in the context of the thread

                        J 1 Reply Last reply
                        0
                        • C Chuck OToole

                          Context dude, gotta read comments in the context of the thread

                          J Offline
                          J Offline
                          jschell
                          wrote on last edited by
                          #13

                          Chuck O'Toole wrote:

                          Context dude, gotta read comments in the context of the thread

                          And respond in that context. Since another poster read your response in way similar to the way I read it, it suggests that your response is the one at fault.

                          1 Reply Last reply
                          0
                          • D David Crow

                            It all boils down to the difference between an array and a pointer. For name, you have set aside room for 20 characters (that are initialized to "Some Name"). For name2, you are pointing to a static piece of memory. That memory cannot be changed.

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                            S Offline
                            S Offline
                            Sivaraman Dhamodharan
                            wrote on last edited by
                            #14

                            Thanks Crow. That is constant piece of memory.

                            Programming Article

                            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