?
Solved

Assigning an array using {}'s

Posted on 2007-04-07
3
Medium Priority
?
193 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

743 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