ark1974
ark1974

Reputation: 655

Struct variable name prefixed with a .(dot) need explaination

This is an extract of code from OBS Studio under GitHub. I failed to understand the followings:

1) Is the struct keyword followed by a struct name (obs_encoder_info) and a tag(opus_encoder_info) ?

2) How a struct variable can be prefixed with a .(dot) ? Is it a member variable of another struct?

struct obs_encoder_info opus_encoder_info = {
   .id             = "ffmpeg_opus",
   .type           = OBS_ENCODER_AUDIO,
   .codec          = "opus",
   .get_name       = opus_getname,
   .create         = opus_create,
   .destroy        = enc_destroy
};

Upvotes: 1

Views: 388

Answers (2)

JaMiT
JaMiT

Reputation: 16843

1) Is the struct keyword followed by a struct name (obs_encoder_info) and a tag(opus_encoder_info) ?

Almost. The struct keyword is followed by a struct name, but after that is a variable (not tag) name. This line declares (and initializes) opus_encoder_info as a variable whose type is struct obs_encoder_info. (In C++ the struct keyword is not required, but in C it is.)

2) How a struct variable can be prefixed with a .(dot) ? Is it a member variable of another struct?

Your terminology is a bit off here, as the struct variable is opus_encoder_info while the things prefixed with a dot are struct members. In any event, a struct obs_encoder_info apparently has members named id, type, etc., and those fields of the opus_encoder_info variable are being initialized via initialization by designators, which was a new feature of C99. (In brief, member x is initialized to value a by writing .x = a inside the braces.)

Yes, I am answering as if the code was written in C even though this question is tagged "C++". I justify that because I have reason to believe the code in question is C. First, there is the use of the struct keyword when declaring a variable – required in C, but superfluous in C++. Second, the code came from a file whose extension is ".c", not ".C" (capitalized) or ".cpp" or any other extension indicating a C++ source file. Third, this kind of code existed in the file in 2014 (there was a name change in 2017), which is rather early to use a C++20 feature, much less use it in production code.

So I have concluded that this question is mis-tagged. However, the presence of an answer (and an accepted answer at that) means that fixing the question's tag could cause more confusion than it would resolve.

Upvotes: 1

songyuanyao
songyuanyao

Reputation: 172894

This is designated initializers (since C++20).

So it declares an object named opus_encoder_info with type obs_encoder_info, and its data member id is initialized with value "ffmpeg_opus", type is initialized with value OBS_ENCODER_AUDIO, and so on.

Upvotes: 3

Related Questions