Is it possible to pass a structure variable as a function argument without previously defining it?

一曲冷凌霜 提交于 2019-12-19 19:40:56

问题


I have two structs defined as so (in color.h):

typedef struct rgb {
  uint8_t r, g, b;
} rgb;

typedef struct hsv {
  float h, s, v;
} hsv;

hsv rgb2hsv(rgb color);
rgb hsv2rgb(hsv color);

I then have the following in main.c which works:

hsv hsvCol = {i/255.0, 1, 1};
rgb col = hsv2rgb(hsvCol);

I want to be able to just create the variable hsvCol inside the parameters for hsv2rgb without having to create the variable and passing it as a parameter.

I've tried the each of the following (in place of the two lines above), sadly none of which compile :(

rgb col = hsv2rgb({i/255.0, 1, 1});
rgb col = hsv2rgb(hsv {i/255.0, 1, 1});
rgb col = hsv2rgb(hsv hsvCol {i/255.0, 1, 1})
rgb col = hsv2rgb(struct hsv {i/255.0, 1, 1});

My question is:

  1. Can I do what I was trying to do at all (but obviously in a different way)?

  2. If 1, how do I go about doing so?


回答1:


You can make use of a compound literal.

Quoting C11, chapter §6.5.2.5, paragraph 3,

A postfix expression that consists of a parenthesized type name followed by a brace enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.

and, paragraph 5,

The value of the compound literal is that of an unnamed object initialized by the initializer list. [...]

So, in your case, the code like

hsv hsvCol = {i/255.0, 1, 1};
rgb col = hsv2rgb(hsvCol);

can be re-written as

rgb col = hsv2rgb( ( hsv ) {i/255.0, 1, 1} );
                    ^^^^    ^^^^^^^^^^^^^
                    |             |
                    |              -- brace enclosed list of initializers
                    -- parenthesized type name


来源:https://stackoverflow.com/questions/42350203/is-it-possible-to-pass-a-structure-variable-as-a-function-argument-without-previ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!