Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a constant array literal to a function that takes a pointer without using a variable C/C++?

Tags:

c++

c

function

If I have a prototype that looks like this:

function(float,float,float,float)

I can pass values like this:

function(1,2,3,4);

So if my prototype is this:

function(float*);

Is there any way I can achieve something like this?

function( {1,2,3,4} );

Just looking for a lazy way to do this without creating a temporary variable, but I can't seem to nail the syntax.

like image 793
sinoth Avatar asked Aug 13 '09 01:08

sinoth


3 Answers

You can do it in C99 (but not ANSI C (C90) or any current variant of C++) with compound literals. See section 6.5.2.5 of the C99 standard for the gory details. Here's an example:

// f is a static array of at least 4 floats
void foo(float f[static 4])
{
   ...
}

int main(void)
{
    foo((float[4]){1.0f, 2.0f, 3.0f, 4.0f});  // OK
    foo((float[5]){1.0f, 2.0f, 3.0f, 4.0f, 5.0f});  // also OK, fifth element is ignored
    foo((float[3]){1.0f, 2.0f, 3.0f});  // error, although the GCC doesn't complain
    return 0;
}

GCC also provides this as an extension to C90. If you compile with -std=gnu90 (the default), -std=c99, or -std=gnu99, it will compile; if you compile with -std=c90, it will not.

like image 115
Adam Rosenfield Avatar answered Nov 20 '22 10:11

Adam Rosenfield


This is marked both C and C++, so you're gonna get radically different answers.

If you are expecting four parameters, you can do this:

void foo(float f[])
{
    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    float f[] = {1, 2, 3, 4};
    foo(f);
}

But that is rather unsafe, as you could do this by accident:

void foo(float f[])
{
    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    float f[] = {1, 2}; // uh-oh
    foo(f);
}

It is usually best to leave them as individual parameters. Since you shouldn't be using raw arrays anyway, you can do this:

#include <cassert>
#include <vector>

void foo(std::vector<float> f)
{
    assert(f.size() == 4);

    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    float f[] = {1, 2, 3, 4};
    foo(std::vector<float>(f, f + 4)); // be explicit about size

    // assert says you cannot do this:
    foo(std::vector<float>(f, f + 2));
}

An improvement, but not much of one. You could use boost::array, but rather than an error for mismatched size, they are initialized to 0:

#include <boost/array.hpp>

void foo(boost::array<float, 4> f)
{
    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    boost::array<float, 4> f = {1, 2, 3, 4};
    foo(f);

    boost::array<float, 4> f2 = {1, 2}; // same as = {1, 2, 0, 0}
    foo(f2);
}

This will all be fixed in C++0x, when initializer list constructors are added:

#include <cassert>
#include <vector>

void foo(std::vector<float> f)
{
    assert(f.size() == 4);

    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    foo({1, 2, 3, 4}); // yay, construct vector from this

    // assert says you cannot do this:
    foo({1, 2});
}

And probably boost::array as well:

#include <boost/array.hpp>

void foo(boost::array<float, 4> f)
{
    float f0 = f[0];
    float f1 = f[1];
    float f2 = f[2];
    float f3 = f[3];
}

int main(void)
{
    foo({1, 2, 3, 4});

    foo({1, 2}); // same as = {1, 2, 0, 0} ..? I'm not sure,
                 // I don't know if they will do the check, if possible.
}
like image 33
GManNickG Avatar answered Nov 20 '22 09:11

GManNickG


You can create a compound literal:

function ((float[2]){2.0, 4.0});

Although, I'm not sure why you want to go through the trouble. This is not permitted by ISO.

Generally, shortcuts like this should be avoided in favor of readability in all cases; laziness is not a good habit to explore (personal opinion, of course)

like image 5
ezpz Avatar answered Nov 20 '22 09:11

ezpz