How to pass a structural compound literal as a parameter to a function?


Cheeseman 69

Here is structwhat I have:

typedef struct 
{
    float r, g, b;
} color_t;

I want to pass a compound literal of this structas a parameter to a function like this:

void printcolor(color_t c)
{
    printf("color is : %f %f %f\n", c.r, c.g, c.b);
}

printcolor({1.0f, 0.6f, 0.8f});

However, this gives me an error:

error: expected expression before '{' token

Adrian Mole

A compound literal in C must have the specified type (using the 'cast-like' syntax) before the brace-enclosed initializer list. So, as mentioned in the comments, you should change the function call to:

printcolor((color_t){1.0f, 0.6f, 0.8f});

Related


Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

How to pass a function to an array literal?

other How can I pass an array without making it a separate variable? For example, I know this works: class Test{ public static void main(String[] args){ String[] arbitraryStrings={"foo"}; takesStringArray(arbitraryStrings); } public

How to pass literal parameter to included js?

Majidi Karachi How to pass literal parameter {$currchoice.id}to js? I have the following currency picker and I need it to pass the selected value to javascript as the param (currency=2)is(currency={$currchoice.id}) <!-- Currency --> {if !$loggedin && co

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Compound literal pointer passed as parameter in C

Function Is pass-by-value cast pointer valid in C? If not why does this work? #include <stdio.h> typedef struct { int size; char* str; } MY_STRUCT; void print_my_struct_prt(MY_STRUCT* mstrct) { printf("%s%d%s%s\n", "size: ", mstrct->size, " c

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

Get address of temporary (compound literal) parameter in C

bee rope I can't imagine this isn't a duplicate yet, but I can't find the answer easily, as more complex scenarios specific to C++ seem to dominate discussion 0 . Is it legal to take a temporary address constructed in the parameter list of a function call in C

How to pass function as parameter

epinephrine I have a function that takes a vector and sums all elements. (def rec (fn [numbers acc] (if (empty? numbers) acc (recur (rest numbers) (+ acc (first numbers)))))) (prn (rec [1 2 3] 0)) However, instead of calling the function "+"

How to pass function as parameter

epinephrine I have a function that takes a vector and sums all elements. (def rec (fn [numbers acc] (if (empty? numbers) acc (recur (rest numbers) (+ acc (first numbers)))))) (prn (rec [1 2 3] 0)) However, instead of calling the function "+"

How to pass function as parameter

epinephrine I have a function that takes a vector and sums all elements. (def rec (fn [numbers acc] (if (empty? numbers) acc (recur (rest numbers) (+ acc (first numbers)))))) (prn (rec [1 2 3] 0)) However, instead of calling the function "+"

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass parameters in function object literal

CalAlt I want to pass integers as min and max parameters to a function to return a random number for use by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not a functi

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently