Assigning values to all the elements of a dynamically allocated pointer array?











up vote
4
down vote

favorite












I have a pointer array (ptrArr1) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.



int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work


deleteptrArr1;
return 0;

}









share|improve this question




















  • 1




    When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
    – Scheff
    Nov 20 at 7:59












  • Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
    – Jabberwocky
    Nov 20 at 8:27















up vote
4
down vote

favorite












I have a pointer array (ptrArr1) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.



int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work


deleteptrArr1;
return 0;

}









share|improve this question




















  • 1




    When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
    – Scheff
    Nov 20 at 7:59












  • Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
    – Jabberwocky
    Nov 20 at 8:27













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have a pointer array (ptrArr1) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.



int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work


deleteptrArr1;
return 0;

}









share|improve this question















I have a pointer array (ptrArr1) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.



int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work


deleteptrArr1;
return 0;

}






c++ arrays pointers dynamic-memory-allocation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:14









Micha Wiedenmann

10.1k1364102




10.1k1364102










asked Nov 20 at 7:55









Carlos Robin Alvarenga

376




376








  • 1




    When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
    – Scheff
    Nov 20 at 7:59












  • Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
    – Jabberwocky
    Nov 20 at 8:27














  • 1




    When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
    – Some programmer dude
    Nov 20 at 7:59






  • 1




    ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
    – Scheff
    Nov 20 at 7:59












  • Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
    – Jabberwocky
    Nov 20 at 8:27








1




1




When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
– Some programmer dude
Nov 20 at 7:59




When you do ptrArr1 = &one it's similar to doing one = two and expecting the value of one to still be 1.
– Some programmer dude
Nov 20 at 7:59




1




1




Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
– Some programmer dude
Nov 20 at 7:59




Furthermore, ptrArr[x] is an int and not an int* (which e.g. &one is).
– Some programmer dude
Nov 20 at 7:59




1




1




ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
– Scheff
Nov 20 at 7:59






ptrArr[1] = &two; tries to assign an address (int*) to an int -> type mismatch. ptrArr1 + 1 = &two; is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;.
– Scheff
Nov 20 at 7:59














Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
– Jabberwocky
Nov 20 at 8:27




Also you have int *ptrArr1 = new int[2]; and right after ptrArr1 = &one; which overwrites ptrArr1. It's kind of like writing foo = 2; foo = 42;.
– Jabberwocky
Nov 20 at 8:27












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You have an int array, not a pointer array. You can create a pointer array using



int **ptrArr1 = new int*[2];


and then assign the pointers to each pointer in the array:



ptrArr1[0] = &one;
ptrArr1[1] = &two;





share|improve this answer

















  • 2




    Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
    – Carlos Robin Alvarenga
    Nov 20 at 8:09










  • @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
    – Scheff
    Nov 20 at 8:50


















up vote
5
down vote













There is a difference between an array of integers and an array of pointers to int. In your case ptrArr1 is a pointer to an array of integers with space for two integers. Therefore you can only assign an int to ptrArr1[1] = 2 but not an address. Compare



int xs = { 1, 2, 3 };    // an array of integers

int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers


Now you could also have pointers pointing to the first element of xs resp. ys:



int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];

// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;


For the simplification step you should look into What is array decaying?






share|improve this answer























    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%2f53388495%2fassigning-values-to-all-the-elements-of-a-dynamically-allocated-pointer-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    You have an int array, not a pointer array. You can create a pointer array using



    int **ptrArr1 = new int*[2];


    and then assign the pointers to each pointer in the array:



    ptrArr1[0] = &one;
    ptrArr1[1] = &two;





    share|improve this answer

















    • 2




      Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
      – Carlos Robin Alvarenga
      Nov 20 at 8:09










    • @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
      – Scheff
      Nov 20 at 8:50















    up vote
    3
    down vote



    accepted










    You have an int array, not a pointer array. You can create a pointer array using



    int **ptrArr1 = new int*[2];


    and then assign the pointers to each pointer in the array:



    ptrArr1[0] = &one;
    ptrArr1[1] = &two;





    share|improve this answer

















    • 2




      Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
      – Carlos Robin Alvarenga
      Nov 20 at 8:09










    • @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
      – Scheff
      Nov 20 at 8:50













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    You have an int array, not a pointer array. You can create a pointer array using



    int **ptrArr1 = new int*[2];


    and then assign the pointers to each pointer in the array:



    ptrArr1[0] = &one;
    ptrArr1[1] = &two;





    share|improve this answer












    You have an int array, not a pointer array. You can create a pointer array using



    int **ptrArr1 = new int*[2];


    and then assign the pointers to each pointer in the array:



    ptrArr1[0] = &one;
    ptrArr1[1] = &two;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 7:59









    eozd

    8941515




    8941515








    • 2




      Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
      – Carlos Robin Alvarenga
      Nov 20 at 8:09










    • @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
      – Scheff
      Nov 20 at 8:50














    • 2




      Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
      – Carlos Robin Alvarenga
      Nov 20 at 8:09










    • @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
      – Scheff
      Nov 20 at 8:50








    2




    2




    Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
    – Carlos Robin Alvarenga
    Nov 20 at 8:09




    Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
    – Carlos Robin Alvarenga
    Nov 20 at 8:09












    @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
    – Scheff
    Nov 20 at 8:50




    @CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
    – Scheff
    Nov 20 at 8:50












    up vote
    5
    down vote













    There is a difference between an array of integers and an array of pointers to int. In your case ptrArr1 is a pointer to an array of integers with space for two integers. Therefore you can only assign an int to ptrArr1[1] = 2 but not an address. Compare



    int xs = { 1, 2, 3 };    // an array of integers

    int y0 = 42;
    int *ys = { &y0, &y0 }; // an array of pointers to integers


    Now you could also have pointers pointing to the first element of xs resp. ys:



    int *ptr_xs = &xs[0];
    int **ptr_ys = &ys[0];

    // which can be simplified to:
    int *ptr_xs = xs;
    int **ptr_ys = ys;


    For the simplification step you should look into What is array decaying?






    share|improve this answer



























      up vote
      5
      down vote













      There is a difference between an array of integers and an array of pointers to int. In your case ptrArr1 is a pointer to an array of integers with space for two integers. Therefore you can only assign an int to ptrArr1[1] = 2 but not an address. Compare



      int xs = { 1, 2, 3 };    // an array of integers

      int y0 = 42;
      int *ys = { &y0, &y0 }; // an array of pointers to integers


      Now you could also have pointers pointing to the first element of xs resp. ys:



      int *ptr_xs = &xs[0];
      int **ptr_ys = &ys[0];

      // which can be simplified to:
      int *ptr_xs = xs;
      int **ptr_ys = ys;


      For the simplification step you should look into What is array decaying?






      share|improve this answer

























        up vote
        5
        down vote










        up vote
        5
        down vote









        There is a difference between an array of integers and an array of pointers to int. In your case ptrArr1 is a pointer to an array of integers with space for two integers. Therefore you can only assign an int to ptrArr1[1] = 2 but not an address. Compare



        int xs = { 1, 2, 3 };    // an array of integers

        int y0 = 42;
        int *ys = { &y0, &y0 }; // an array of pointers to integers


        Now you could also have pointers pointing to the first element of xs resp. ys:



        int *ptr_xs = &xs[0];
        int **ptr_ys = &ys[0];

        // which can be simplified to:
        int *ptr_xs = xs;
        int **ptr_ys = ys;


        For the simplification step you should look into What is array decaying?






        share|improve this answer














        There is a difference between an array of integers and an array of pointers to int. In your case ptrArr1 is a pointer to an array of integers with space for two integers. Therefore you can only assign an int to ptrArr1[1] = 2 but not an address. Compare



        int xs = { 1, 2, 3 };    // an array of integers

        int y0 = 42;
        int *ys = { &y0, &y0 }; // an array of pointers to integers


        Now you could also have pointers pointing to the first element of xs resp. ys:



        int *ptr_xs = &xs[0];
        int **ptr_ys = &ys[0];

        // which can be simplified to:
        int *ptr_xs = xs;
        int **ptr_ys = ys;


        For the simplification step you should look into What is array decaying?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 8:21

























        answered Nov 20 at 7:59









        Micha Wiedenmann

        10.1k1364102




        10.1k1364102






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388495%2fassigning-values-to-all-the-elements-of-a-dynamically-allocated-pointer-array%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

            Wiesbaden

            Marschland

            Dieringhausen