Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a dot before the variable name in struct mean?

Tags:

c

linux

kernel

looking at the linux kernel source, I found this:

static struct tty_operations serial_ops = {   .open = tiny_open,   .close = tiny_close,   .write = tiny_write,   .write_room = tiny_write_room,   .set_termios = tiny_set_termios, }; 

I've never seen such a notation in C. Why is there a dot before the variable name?

like image 718
c0de Avatar asked Sep 20 '11 15:09

c0de


People also ask

Can a variable name start with a dot?

A valid variable name consists of letters, numbers and the dot or underline characters. The variable name starts with a letter or the dot not followed by a number.

What is a struct variable?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

Can struct name be same as variable name?

Yes, that is perfectly fine.


1 Answers

This is a Designated Initializer, which is syntax added for C99. Relevant excerpt:

In a structure initializer, specify the name of a field to initialize with ‘.fieldname =’ before the element value. For example, given the following structure,

struct point { int x, y; };  

the following initialization

struct point p = { .y = yvalue, .x = xvalue };  

is equivalent to

struct point p = { xvalue, yvalue }; 
like image 67
Reed Copsey Avatar answered Oct 12 '22 23:10

Reed Copsey