Array of Pointers Pointing to an unknown location











up vote
2
down vote

favorite












I was practicing with array of pointers.
i wrote a simple code.



#include<iostream>
using namespace std;

int main(){

int a[3]={1,2,3};
int *b[3];

for(int i=0; i<3; i++){
b[i] = &a[i];
}

cout<<b; //This gives a confusing output
cout<<endl;

for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}


and Output is



0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38


Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.










share|improve this question






















  • what did you expect b to be?
    – mangusta
    Nov 9 at 5:30










  • It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
    – Shaida Muhammad
    Nov 9 at 5:33










  • so what is the result of cout<<b and why do you think it is confusing?
    – mangusta
    Nov 9 at 5:34












  • I wanna know what b pointing in Memory. I mean what is inside address b.
    – Shaida Muhammad
    Nov 9 at 5:35








  • 2




    b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
    – molbdnilo
    Nov 9 at 5:39















up vote
2
down vote

favorite












I was practicing with array of pointers.
i wrote a simple code.



#include<iostream>
using namespace std;

int main(){

int a[3]={1,2,3};
int *b[3];

for(int i=0; i<3; i++){
b[i] = &a[i];
}

cout<<b; //This gives a confusing output
cout<<endl;

for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}


and Output is



0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38


Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.










share|improve this question






















  • what did you expect b to be?
    – mangusta
    Nov 9 at 5:30










  • It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
    – Shaida Muhammad
    Nov 9 at 5:33










  • so what is the result of cout<<b and why do you think it is confusing?
    – mangusta
    Nov 9 at 5:34












  • I wanna know what b pointing in Memory. I mean what is inside address b.
    – Shaida Muhammad
    Nov 9 at 5:35








  • 2




    b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
    – molbdnilo
    Nov 9 at 5:39













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I was practicing with array of pointers.
i wrote a simple code.



#include<iostream>
using namespace std;

int main(){

int a[3]={1,2,3};
int *b[3];

for(int i=0; i<3; i++){
b[i] = &a[i];
}

cout<<b; //This gives a confusing output
cout<<endl;

for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}


and Output is



0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38


Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.










share|improve this question













I was practicing with array of pointers.
i wrote a simple code.



#include<iostream>
using namespace std;

int main(){

int a[3]={1,2,3};
int *b[3];

for(int i=0; i<3; i++){
b[i] = &a[i];
}

cout<<b; //This gives a confusing output
cout<<endl;

for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}


and Output is



0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38


Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.







c++ arrays pointers output cout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 5:24









Shaida Muhammad

609




609












  • what did you expect b to be?
    – mangusta
    Nov 9 at 5:30










  • It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
    – Shaida Muhammad
    Nov 9 at 5:33










  • so what is the result of cout<<b and why do you think it is confusing?
    – mangusta
    Nov 9 at 5:34












  • I wanna know what b pointing in Memory. I mean what is inside address b.
    – Shaida Muhammad
    Nov 9 at 5:35








  • 2




    b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
    – molbdnilo
    Nov 9 at 5:39


















  • what did you expect b to be?
    – mangusta
    Nov 9 at 5:30










  • It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
    – Shaida Muhammad
    Nov 9 at 5:33










  • so what is the result of cout<<b and why do you think it is confusing?
    – mangusta
    Nov 9 at 5:34












  • I wanna know what b pointing in Memory. I mean what is inside address b.
    – Shaida Muhammad
    Nov 9 at 5:35








  • 2




    b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
    – molbdnilo
    Nov 9 at 5:39
















what did you expect b to be?
– mangusta
Nov 9 at 5:30




what did you expect b to be?
– mangusta
Nov 9 at 5:30












It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33




It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33












so what is the result of cout<<b and why do you think it is confusing?
– mangusta
Nov 9 at 5:34






so what is the result of cout<<b and why do you think it is confusing?
– mangusta
Nov 9 at 5:34














I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35






I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35






2




2




b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
– molbdnilo
Nov 9 at 5:39




b is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
– molbdnilo
Nov 9 at 5:39












4 Answers
4






active

oldest

votes

















up vote
4
down vote



accepted










b is an array of pointers to int. It's address is the address of the first pointer.




  • The first pointer b[0] is assigned the address of element a[0] (so b[0] points to 1).


  • b[1] is assigned the address of a[1] (so b[1] points to 2)


  • b[2] is assigned the address of a[2] (so b[2] points to 3)


In each case the pointer in the b array holds the address of the corresponding element within a. So if there are any changes made to the elements of a after your loop assigning the address in a to b, the value held in the memory location pointed to by the element of b will change, but the address for that value held by b will remain unchanged.






share|improve this answer




























    up vote
    3
    down vote













    When b is passed to cout's << operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b is the same as printing out &b[0].



    Therefore, cout << b; prints out the address of the first pointer in the b[3] array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)






    share|improve this answer























    • It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
      – alter igel
      Nov 9 at 5:49










    • Good point, I'll update my answer :)
      – Jeremy Friesner
      Nov 9 at 5:50




















    up vote
    3
    down vote













    b is an array of pointers to int which is a local variable on the stack.



    Its address does not depend on what individual elements of its array are assigned to.



    If you print its value before the for loop (where you assign values to its members), you will see that it is the same as the value printed after the for loop.



    See live demo here.



    Pictorially:



    enter image description here






    share|improve this answer






























      up vote
      2
      down vote













      b Memory adress of the whole array.






      share|improve this answer





















      • I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
        – Shaida Muhammad
        Nov 9 at 5:42










      • If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
        – Allamo Olsson
        Nov 9 at 5:44












      • Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
        – Jeremy Friesner
        Nov 9 at 5:59











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220305%2farray-of-pointers-pointing-to-an-unknown-location%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      b is an array of pointers to int. It's address is the address of the first pointer.




      • The first pointer b[0] is assigned the address of element a[0] (so b[0] points to 1).


      • b[1] is assigned the address of a[1] (so b[1] points to 2)


      • b[2] is assigned the address of a[2] (so b[2] points to 3)


      In each case the pointer in the b array holds the address of the corresponding element within a. So if there are any changes made to the elements of a after your loop assigning the address in a to b, the value held in the memory location pointed to by the element of b will change, but the address for that value held by b will remain unchanged.






      share|improve this answer

























        up vote
        4
        down vote



        accepted










        b is an array of pointers to int. It's address is the address of the first pointer.




        • The first pointer b[0] is assigned the address of element a[0] (so b[0] points to 1).


        • b[1] is assigned the address of a[1] (so b[1] points to 2)


        • b[2] is assigned the address of a[2] (so b[2] points to 3)


        In each case the pointer in the b array holds the address of the corresponding element within a. So if there are any changes made to the elements of a after your loop assigning the address in a to b, the value held in the memory location pointed to by the element of b will change, but the address for that value held by b will remain unchanged.






        share|improve this answer























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          b is an array of pointers to int. It's address is the address of the first pointer.




          • The first pointer b[0] is assigned the address of element a[0] (so b[0] points to 1).


          • b[1] is assigned the address of a[1] (so b[1] points to 2)


          • b[2] is assigned the address of a[2] (so b[2] points to 3)


          In each case the pointer in the b array holds the address of the corresponding element within a. So if there are any changes made to the elements of a after your loop assigning the address in a to b, the value held in the memory location pointed to by the element of b will change, but the address for that value held by b will remain unchanged.






          share|improve this answer












          b is an array of pointers to int. It's address is the address of the first pointer.




          • The first pointer b[0] is assigned the address of element a[0] (so b[0] points to 1).


          • b[1] is assigned the address of a[1] (so b[1] points to 2)


          • b[2] is assigned the address of a[2] (so b[2] points to 3)


          In each case the pointer in the b array holds the address of the corresponding element within a. So if there are any changes made to the elements of a after your loop assigning the address in a to b, the value held in the memory location pointed to by the element of b will change, but the address for that value held by b will remain unchanged.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 5:47









          David C. Rankin

          38.9k32546




          38.9k32546
























              up vote
              3
              down vote













              When b is passed to cout's << operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b is the same as printing out &b[0].



              Therefore, cout << b; prints out the address of the first pointer in the b[3] array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)






              share|improve this answer























              • It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
                – alter igel
                Nov 9 at 5:49










              • Good point, I'll update my answer :)
                – Jeremy Friesner
                Nov 9 at 5:50

















              up vote
              3
              down vote













              When b is passed to cout's << operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b is the same as printing out &b[0].



              Therefore, cout << b; prints out the address of the first pointer in the b[3] array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)






              share|improve this answer























              • It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
                – alter igel
                Nov 9 at 5:49










              • Good point, I'll update my answer :)
                – Jeremy Friesner
                Nov 9 at 5:50















              up vote
              3
              down vote










              up vote
              3
              down vote









              When b is passed to cout's << operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b is the same as printing out &b[0].



              Therefore, cout << b; prints out the address of the first pointer in the b[3] array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)






              share|improve this answer














              When b is passed to cout's << operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b is the same as printing out &b[0].



              Therefore, cout << b; prints out the address of the first pointer in the b[3] array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 9 at 5:57

























              answered Nov 9 at 5:46









              Jeremy Friesner

              37.8k1077157




              37.8k1077157












              • It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
                – alter igel
                Nov 9 at 5:49










              • Good point, I'll update my answer :)
                – Jeremy Friesner
                Nov 9 at 5:50




















              • It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
                – alter igel
                Nov 9 at 5:49










              • Good point, I'll update my answer :)
                – Jeremy Friesner
                Nov 9 at 5:50


















              It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
              – alter igel
              Nov 9 at 5:49




              It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
              – alter igel
              Nov 9 at 5:49












              Good point, I'll update my answer :)
              – Jeremy Friesner
              Nov 9 at 5:50






              Good point, I'll update my answer :)
              – Jeremy Friesner
              Nov 9 at 5:50












              up vote
              3
              down vote













              b is an array of pointers to int which is a local variable on the stack.



              Its address does not depend on what individual elements of its array are assigned to.



              If you print its value before the for loop (where you assign values to its members), you will see that it is the same as the value printed after the for loop.



              See live demo here.



              Pictorially:



              enter image description here






              share|improve this answer



























                up vote
                3
                down vote













                b is an array of pointers to int which is a local variable on the stack.



                Its address does not depend on what individual elements of its array are assigned to.



                If you print its value before the for loop (where you assign values to its members), you will see that it is the same as the value printed after the for loop.



                See live demo here.



                Pictorially:



                enter image description here






                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  b is an array of pointers to int which is a local variable on the stack.



                  Its address does not depend on what individual elements of its array are assigned to.



                  If you print its value before the for loop (where you assign values to its members), you will see that it is the same as the value printed after the for loop.



                  See live demo here.



                  Pictorially:



                  enter image description here






                  share|improve this answer














                  b is an array of pointers to int which is a local variable on the stack.



                  Its address does not depend on what individual elements of its array are assigned to.



                  If you print its value before the for loop (where you assign values to its members), you will see that it is the same as the value printed after the for loop.



                  See live demo here.



                  Pictorially:



                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 8:02

























                  answered Nov 9 at 5:34









                  P.W

                  7,6802438




                  7,6802438






















                      up vote
                      2
                      down vote













                      b Memory adress of the whole array.






                      share|improve this answer





















                      • I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                        – Shaida Muhammad
                        Nov 9 at 5:42










                      • If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                        – Allamo Olsson
                        Nov 9 at 5:44












                      • Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                        – Jeremy Friesner
                        Nov 9 at 5:59















                      up vote
                      2
                      down vote













                      b Memory adress of the whole array.






                      share|improve this answer





















                      • I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                        – Shaida Muhammad
                        Nov 9 at 5:42










                      • If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                        – Allamo Olsson
                        Nov 9 at 5:44












                      • Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                        – Jeremy Friesner
                        Nov 9 at 5:59













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      b Memory adress of the whole array.






                      share|improve this answer












                      b Memory adress of the whole array.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 9 at 5:40









                      Allamo Olsson

                      849




                      849












                      • I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                        – Shaida Muhammad
                        Nov 9 at 5:42










                      • If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                        – Allamo Olsson
                        Nov 9 at 5:44












                      • Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                        – Jeremy Friesner
                        Nov 9 at 5:59


















                      • I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                        – Shaida Muhammad
                        Nov 9 at 5:42










                      • If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                        – Allamo Olsson
                        Nov 9 at 5:44












                      • Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                        – Jeremy Friesner
                        Nov 9 at 5:59
















                      I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                      – Shaida Muhammad
                      Nov 9 at 5:42




                      I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
                      – Shaida Muhammad
                      Nov 9 at 5:42












                      If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                      – Allamo Olsson
                      Nov 9 at 5:44






                      If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
                      – Allamo Olsson
                      Nov 9 at 5:44














                      Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                      – Jeremy Friesner
                      Nov 9 at 5:59




                      Don't forget there are two arrays in the program; a and b. The a array starts at 0x6ffe30 and is 12 bytes long (3 4-byte ints), whereas the b array starts at 0x6ffe10 and is 24 bytes long (3 8-byte pointers)
                      – Jeremy Friesner
                      Nov 9 at 5:59


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220305%2farray-of-pointers-pointing-to-an-unknown-location%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)