Solved

Assigning an array using {}'s

Posted on 2007-04-07
3
182 Views
Last Modified: 2013-12-14
I have an array of floats that is declared like:

float x1[3];

I then later want to do something like:

x1 = {1.5,3.2,4.5};

but it seems I can only do that on initialisation. Is there some way to do this such as using an array copy routine?
0
Comment
Question by:MrModest
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 18869523
>> but it seems I can only do that on initialisation.

Indeed. The { } syntax for array initialization can only be used when defining/declaring the array :

      float x1[3] = {1.5,3.2,4.5};

You could do something like this if you really need it :

    float x1[3] = { 0.0 };  // <---- initialize to all 0's

    {
        float x1_tmp[3] = {1.5,3.2,4.5};                // <---- create a temporary array with the values we want ...
        memcpy(x1, x1_tmp, 3 * sizeof(float));      // <---- copy that temporary array into the x1 array
    }

    for (int i = 0; i < 3; ++i) {     // <---- the x1 array now contains the desired values
        cout << x1[i] << endl;
    }


Of course you could also use a function to populate your array :

    void populate_array(float (&x1)[3]) {
        float x1_tmp[3] = {1.5,3.2,4.5};                // <---- create a temporary array with the values we want ...
        memcpy(x1, x1_tmp, 3 * sizeof(float));      // <---- copy that temporary array into the x1 array
        return;
    }

and call it like this :

    float x1[3] = { 0.0 };  // <---- initialize to all 0's

    populate_array(x1);

    for (int i = 0; i < 3; ++i) {     // <---- the x1 array now contains the desired values
        cout << x1[i] << endl;
    }
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18869528
Oh, and a good idea is to inline that function :

    inline void populate_array(float (&x1)[3]) {
        float x1_tmp[3] = {1.5,3.2,4.5};                // <---- create a temporary array with the values we want ...
        memcpy(x1, x1_tmp, 3 * sizeof(float));      // <---- copy that temporary array into the x1 array
        return;
    }
0
 

Author Comment

by:MrModest
ID: 18869538
Thanks, I figured I would have to do something like that.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question