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 array v.s. int array

string array v.s. int array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
12 Posts 4 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.
  • G George_George

    Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array; 2. When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
    int main() { wchar_t me[] = L"Hello World \n"; wchar_t (*me2_ptr)[14] = &me; wcout << *me2_ptr << endl; // output Hello World wcout << **me2_ptr << endl; // output H int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 }; int (*pval)[10] = &values; wcout << *pval << endl; // output 0x0017f6f0 wcout << **pval << endl; // output 10 return 0; }
    thanks in advance, George

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

    The depicted behavior is not so strange...

    George_George wrote:

    1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array;

    In fact the behavior is quite similar: the name (or the address-of operator & applied on the name, remembering past posts... :) ) of the int array returns the address of the memory containing the integer values as well the name of the character array returns the address of the memory containing the character values; you find a noticeable difference in the output only because a (C-like) string is nothing more than the pointer to the memory holding the zero-terminated character sequence, hence whenever wcout finds a (wide) character pointer, it prints the characters belonging to the pointed memory.

    George_George wrote:

    When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?

    Again, nothing strange here. Given a int pointer, say int *p, deferencing it means: "take the content of the pointed memory", namely the integer value at address p (that coincide with the first int element of the array). The same point applies to the character array. :) :)

    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.

    G 1 Reply Last reply
    0
    • D Doc Lobster

      Hi, I'm not exactly sure why you are using wchar_t (*me2_ptr)[14] = &me; and its equivalent for the int. However, look at things like this: me2_ptr - is a pointer on a pointer on a wchar_t *me2_ptr - is a pointer on a wchar_t **me2_ptr - is a wchar_t pval - is a pointer on a pointer on a int *pval - is a pointer on a int **pval is a int If you call operator<< on a pointer on a wchar_t it assumes its target is a c-string and delivers the string itself. If you call operator<< on a pointer on a int it just delivers the value of the pointer. This is just the way operator<< is implemented.

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #4

      Thanks Doc, Why *me2_ptr - is a pointer on a pointer on a wchar_t*? I think &me is a pointer to wchar_t, right? Since in the following code, you can see &me is the same as me,int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
      regards, George

      1 Reply Last reply
      0
      • C CPallini

        The depicted behavior is not so strange...

        George_George wrote:

        1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array;

        In fact the behavior is quite similar: the name (or the address-of operator & applied on the name, remembering past posts... :) ) of the int array returns the address of the memory containing the integer values as well the name of the character array returns the address of the memory containing the character values; you find a noticeable difference in the output only because a (C-like) string is nothing more than the pointer to the memory holding the zero-terminated character sequence, hence whenever wcout finds a (wide) character pointer, it prints the characters belonging to the pointed memory.

        George_George wrote:

        When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?

        Again, nothing strange here. Given a int pointer, say int *p, deferencing it means: "take the content of the pointed memory", namely the integer value at address p (that coincide with the first int element of the array). The same point applies to the character array. :) :)

        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.

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #5

        Hi CPallini, What makes me confused is, in my test program before, the address of a string array should be the same as the string array itself. For example below, buf is the same as &buf.int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
        I think in the code posted in the question, me2_ptr = &me, and it is the same as me, without dereferencing it, if we simply wcout << me2_ptr, result should be the same as wcount << me? Why we need to dereferencing it by operator * in order to get the same output of wcout << me? regards, George

        C 1 Reply Last reply
        0
        • G George_George

          Hi CPallini, What makes me confused is, in my test program before, the address of a string array should be the same as the string array itself. For example below, buf is the same as &buf.int main() { char buf[] = "Hello World \n"; _ASSERT (buf == &buf); return 0; }
          I think in the code posted in the question, me2_ptr = &me, and it is the same as me, without dereferencing it, if we simply wcout << me2_ptr, result should be the same as wcount << me? Why we need to dereferencing it by operator * in order to get the same output of wcout << me? regards, George

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

          Because you defined (correctly) it as pointer to an array address (i.e. another pointer), i.e. *me2_ptr contains the address of me (the same address returned by &me). Hence me2_ptr isn't a pointer to w_chars, but instead a pointer to a pointer of w_charss (added: that turns out to be the same address...). :)

          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.

          modified on Tuesday, December 11, 2007 5:41:54 AM

          G 1 Reply Last reply
          0
          • C CPallini

            Because you defined (correctly) it as pointer to an array address (i.e. another pointer), i.e. *me2_ptr contains the address of me (the same address returned by &me). Hence me2_ptr isn't a pointer to w_chars, but instead a pointer to a pointer of w_charss (added: that turns out to be the same address...). :)

            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.

            modified on Tuesday, December 11, 2007 5:41:54 AM

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #7

            Hi CPallini, If you have Visual Studio at hand, you can debug my sample program, and you can find that me is the same as me2_ptr (you can watch it), so me2_ptr is the same as me, I do not know why *me2_ptr is also the same as me? regards, George

            C 1 Reply Last reply
            0
            • G George_George

              Hi CPallini, If you have Visual Studio at hand, you can debug my sample program, and you can find that me is the same as me2_ptr (you can watch it), so me2_ptr is the same as me, I do not know why *me2_ptr is also the same as me? regards, George

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

              You're right. *me2_ptr and me2_ptr both hold the same address (but are different types, this way wcout behaves differently on them), this probably is related to the fact that dereferencing an array name (i.e. *me2_ptr) should return the same address returned by the array name itself. :)

              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.

              G 1 Reply Last reply
              0
              • C CPallini

                You're right. *me2_ptr and me2_ptr both hold the same address (but are different types, this way wcout behaves differently on them), this probably is related to the fact that dereferencing an array name (i.e. *me2_ptr) should return the same address returned by the array name itself. :)

                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.

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #9

                Thanks CPallini, I think you mean, 1. *me2_ptr is value of array type, and wcout is overloaded to print out the string array; 2. me2_ptr is address of array type, and wcount will print the address of array. Right? regards, George

                C 1 Reply Last reply
                0
                • G George_George

                  Thanks CPallini, I think you mean, 1. *me2_ptr is value of array type, and wcout is overloaded to print out the string array; 2. me2_ptr is address of array type, and wcount will print the address of array. Right? regards, George

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

                  Technically the ostream extraction operator << is overloaded, but, poorly speaking the answer is yes. :)

                  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.

                  1 Reply Last reply
                  0
                  • G George_George

                    Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the address of the array; 2. When dereferencing it twice (operator **), why the result is always the 1st element in both string array sample and int array sample?
                    int main() { wchar_t me[] = L"Hello World \n"; wchar_t (*me2_ptr)[14] = &me; wcout << *me2_ptr << endl; // output Hello World wcout << **me2_ptr << endl; // output H int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 }; int (*pval)[10] = &values; wcout << *pval << endl; // output 0x0017f6f0 wcout << **pval << endl; // output 10 return 0; }
                    thanks in advance, George

                    I Offline
                    I Offline
                    Iain Clarke Warrior Programmer
                    wrote on last edited by
                    #11

                    Cpallini has already explained at some length (to me too!) how the address of an array has the same value for convenience to printf (and it's relations). The two bits of code you've shown do the same thing with derefencing. The only difference is that the wcout is clever enough to go "oh, I won't give a number, I'll give a lump of text - aren't I helpful?". The short answer to question 2 BECAUSE. Longer answer... Because it has been defined that way by people cleverer than us. Is this feature causing you problems? Iain.

                    G 1 Reply Last reply
                    0
                    • I Iain Clarke Warrior Programmer

                      Cpallini has already explained at some length (to me too!) how the address of an array has the same value for convenience to printf (and it's relations). The two bits of code you've shown do the same thing with derefencing. The only difference is that the wcout is clever enough to go "oh, I won't give a number, I'll give a lump of text - aren't I helpful?". The short answer to question 2 BECAUSE. Longer answer... Because it has been defined that way by people cleverer than us. Is this feature causing you problems? Iain.

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #12

                      Thanks Iain, My question is answered. regards, George

                      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