memory allocation using new

Discussion in 'C++' started by gopesh patel, Sep 8, 2010.

  1. gopesh patel

    gopesh patel Guest

    Hello all,

    I have written a simple function as follows:

    void test(float &b)
    {
    b = 100;
    }

    int main()
    {
    float *a;
    a = new float[2];
    test(*a);
    delete [] a;
    }

    Now I initialized the first element of array using b =100 inside
    function test().
    But I cant find how to initialize the second element of this array ?
    Could someone help me with the syntax for that ?

    Thanks a lot.
    Gopesh
     
    gopesh patel, Sep 8, 2010
    #1
    1. Advertising

  2. gopesh patel wrote:
    > Hello all,
    >
    > I have written a simple function as follows:
    >
    > void test(float &b)
    > {
    > b = 100;
    > }
    >
    > int main()
    > {
    > float *a;
    > a = new float[2];
    > test(*a);
    > delete [] a;
    > }
    >
    > Now I initialized the first element of array using b =100 inside
    > function test().
    > But I cant find how to initialize the second element of this array ?
    > Could someone help me with the syntax for that ?
    >
    > Thanks a lot.
    > Gopesh


    test(a[1]); // note that test(*a); is equivalent to test(a[0]);

    Cheers,
    Stu
     
    Stuart Golodetz, Sep 8, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gaurav Jain

    Memory allocation using New

    Gaurav Jain, Jun 10, 2005, in forum: C++
    Replies:
    7
    Views:
    572
    Peter Koch Larsen
    Jun 11, 2005
  2. Ken
    Replies:
    24
    Views:
    3,879
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    997
    chris
    Oct 28, 2005
  4. Replies:
    5
    Views:
    399
    Taran
    Jan 5, 2007
  5. Replies:
    6
    Views:
    289
    Stuart Redmann
    May 25, 2007
Loading...

Share This Page