Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Assigning an array using {}'s

Posted on 2007-04-07
3
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  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 viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

604 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