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. string pointer and int pointer

string pointer and int pointer

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
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.
  • R Offline
    R Offline
    rahulcrjk
    wrote on last edited by
    #1

    I am new to C++ Consider the following int number = 5; int *numptr = &number; Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.Also what does '&number' mean when 'number' is a pointer.

    R C C K 4 Replies Last reply
    0
    • R rahulcrjk

      I am new to C++ Consider the following int number = 5; int *numptr = &number; Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.Also what does '&number' mean when 'number' is a pointer.

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      Because the '<<' operator in the ostream class has many implementations. Which one of the implementations is called depends on the arguments in call. Functions like this are referred to as homonymous, if I recall correctly. This means that if you call the operator and provide an integer as argument the value will be sent to the stream. If you provide a char* it is assumed to point to a zero-terminated string and will send all characters beginning with the address pointed to and stopping when a NULL char is detected. Read more about the ostream::operator<<() here[^].

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • R rahulcrjk

        I am new to C++ Consider the following int number = 5; int *numptr = &number; Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.Also what does '&number' mean when 'number' is a pointer.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        rahulcrjk wrote:

        gives the string itself as the output and not the address where the character 'f' is stored

        Because the << operator was overloaded for the char* type and it is implemented to display the string itself. STL provided overloads for a lot of different types and they implemented the way they like (here, it makes much more sense to output the string and not its address).

        rahulcrjk wrote:

        Also what does '&number' mean when 'number' is a pointer.

        I don't really understand what you mean but I guess you are asking what &numptr means (if I use the code you provided earlier) ? In that case it simply means that it is the address of the pointer (remember that pointers are variables too, so they also have an address, which can be stored in another variable, and so on ad infinitum :-D ).

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • R rahulcrjk

          I am new to C++ Consider the following int number = 5; int *numptr = &number; Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.Also what does '&number' mean when 'number' is a pointer.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          rahulcrjk wrote:

          Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored

          good.

          rahulcrjk wrote:

          char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.

          Because: (1) the string layout in memory is

          ADDRESS VALUE
          number 'f'
          number + 1 'i'
          number + 2 'v'
          number + 3 'e'
          number + 4 ' '
          number + 5 'i'
          number + 6 's'
          number + 7 ' '
          number + 8 'a'
          number + 9 ' '
          number + 10 'n'
          number + 11 'u'
          number + 12 'm'
          number + 13 'b'
          number + 14 'e'
          number + 15 'r'
          number + 16 '\0'

          (2) the extraction operator << knows about (1).

          rahulcrjk wrote:

          Also what does '&number' mean when 'number' is a pointer.

          is the address of the variable who holds the pointer. For instance

          int i; // a integer variable
          int * p; // a variable who holds the address of a integer variable
          int * * pp; // a variable who holds the address of a variable pointing to a integer
          int k;

          i = 5;
          p = &i;
          pp = &p;
          k = **pp + 2; // what is the value of k?

          :)

          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.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          1 Reply Last reply
          0
          • R rahulcrjk

            I am new to C++ Consider the following int number = 5; int *numptr = &number; Herecan say, numptr is a pointer to an integer and it has the address of the location where 5 is stored char * number = "five is number"; However here is it right to say that the 'number' is a pointer to a char and stores the address of the first character in the string(f). If it is, then why does the statement, cout<<number; gives the string itself as the output and not the address where the character 'f' is stored.Also what does '&number' mean when 'number' is a pointer.

            K Offline
            K Offline
            kingori
            wrote on last edited by
            #5

            hoo ho, i get your question right, and you have no idea how i struggled with c++ before i coluds understand pointers, thats why they even made java, to eliminate the whole thing about pointers, but the minute you understand the, thats it.. Just know that pointers yes have 2 major refences or usage in c++. It all depends on how you use them, but fact is , they do point to an address in memory, not the actual data. Your question goes like this, if i say int my_number = 5; int* my_pointer = # the output ; cout << my_pointer ; displays an address, sth like 45542945; if i output ; cout << *my_pointer; then i get the number 5; this is to say, my_pointer, is a memory (RAM) address, and the value in *my_pointer is the value 5; if when used with a character / string, the pointer will refence the address of the first character, then when i say char* my_ptr = "this is a string"; cout <<my_ptr. my pointer being an address of the 1st character, then i shoudl see the address output, not the whole string. but thats not the case, why??? was that the question. okay, one point you have right, the other point you are missing is that, a pointer when used to refence an array, it points to the first element of the array. why do you think this is correct; int nums[3] = {11,21,31}; int* nums_ptr = nums; cout<< nums_ptr[0] ; because when you do point an array (a string is a also an array), you can copy the whole array into a pointer one time. and yuo know that an array is placed in (contigous) continuous memory locations, i.e. if 11 is in addres x, then 21 is in x+1, and 31. so for integers if you want the third element of the array in that has been copied to an pointer, if you say nums_ptr[2], you wont get the address of the 3rd element, you'll get the value. but again for integers if you say cout<<nums_ptr; you wont get all the elements of the array displayed on the screen like 112131, no, you get the address. thats the only differents with characters since, an array of characters is treated like a string.. displaying the pointer wont display the adress of the fisrt character but the whole string. try play with pointers, that's the weak and stronghold with c++, in that learning is abit confusing but once learnt it gives some crazy flexibility you wont get from any of it's neighbors..

            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