Solved

Assigning an array using {}'s

Posted on 2007-04-07
3
178 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now