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. Simple pointer question...

Simple pointer question...

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
5 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.
  • H Offline
    H Offline
    Haroon Sarwar
    wrote on last edited by
    #1

    whats the difference between char test[] = "Hello"; and char* test = "Hello"; in both cases is "Hello" allocated on the stack?? :confused:

    N CPalliniC M D 4 Replies Last reply
    0
    • H Haroon Sarwar

      whats the difference between char test[] = "Hello"; and char* test = "Hello"; in both cases is "Hello" allocated on the stack?? :confused:

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

      Haroon Sarwar wrote:

      char test[] = "Hello";

      This is an array of characters. This is not a pointer.

      Haroon Sarwar wrote:

      char* test = "Hello";

      This is a pointer to an array of characters. Note that test will contain the address of the given array and not the real data. For proof enter the following in the debugger &test test ptest &ptest You will see that the first two are same, but the last two are not.


      Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

      1 Reply Last reply
      0
      • H Haroon Sarwar

        whats the difference between char test[] = "Hello"; and char* test = "Hello"; in both cases is "Hello" allocated on the stack?? :confused:

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        There are notable differences between:

        Haroon Sarwar wrote:

        char test[] = "Hello";

        Here test is a character array(i.e. a const pointer to a memory area containing six characters -remember the '\0' string terminator-), initialized with the string literal "Hello" .

        Haroon Sarwar wrote:

        char* test = "Hello";

        Here test is a pointer to the string literal "Hello", that is a read-only memory area. See the following sample code:

        char test1[] = "Hello";
        char * test2 = "Hello";

        test1[2]='p'; // (1) legal, you can change the value of an element of the array
        // test1 = test2; (2) ILLEGAL, compiler error, test1 is a const pointer

        //test2[2]='p'; (3) ILLEGAL, runtime error, you cannot change read-only memory area
        test2 = test1;// (4) legal, you can change the pointer value
        test2[2]='p'; // (5) legal, since now, test2 points to the same writable memory area of test1

        Hope that helps :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • H Haroon Sarwar

          whats the difference between char test[] = "Hello"; and char* test = "Hello"; in both cases is "Hello" allocated on the stack?? :confused:

          M Offline
          M Offline
          Matthew Faithfull
          wrote on last edited by
          #4

          If you do these within a function they will both get allocated on the Stack, one array and one pointer. The actual "Hello" text in each case will end up as static data within the executable image. char text[] = "Hello"; will give you an on stack copy of this data at runtime. const char* test = "Hello"; will give you a pointer to the string stored in the image memory. char* test = "Hello"; will almost certainly be the same. If you do these at file scope, i.e. in a .c or .cpp file but not in a function then the "Hello" text is still stored in the image and in this case so is test[] or test. With a reasonable optimising compiler with string merging char* test; will probably end up pointing to the same memory refered to by char test[] which will be the one and only original "Hello" text in the loaded image. Have a look in the debugger at runtime and see what the address values are in each case. You'll soon spot the pointers to stack memory as opposed to pointers into the loaded image, the ranges will be quite different.:)

          Nothing is exactly what it seems but everything with seems can be unpicked.

          1 Reply Last reply
          0
          • H Haroon Sarwar

            whats the difference between char test[] = "Hello"; and char* test = "Hello"; in both cases is "Hello" allocated on the stack?? :confused:

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

            See here.


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            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