Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of structure having only one element?

Example:

struct dummy 
{
    int var;
};

Why structures like this are used? Mostly I have seen them in some header files.

The atomic_t type is also defined like this. Can't it be defined simply using:

typedef int atomic_t;
like image 963
Don't You Worry Child Avatar asked Sep 18 '13 07:09

Don't You Worry Child


People also ask

What is the purpose of using structure?

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 two elements within a structure have the same name?

Yes you can use the variable with same name in different structure. struct second { int x; int y; int the_same }; x,y and the_same are element of structure second. compiler will refer this variable with there structure name not individually..

Are those structures that have one or more pointers?

Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.

What are elements of a struct called?

Structure elements, called 'members', are arranged sequentially, with the members occupying successive locations in memory.


1 Answers

Besides extensibility, this idiom also makes it syntactically impossible to do normal arithmetic on types whose meaning is such that that doesn’t make sense semantically.

E.g.:

typedef uint32_t myObject;
myObject x, y;
...
y = x + 3; // meaningless, but doesn’t produce an error.
           // may later cause runtime failure.

v.s.

typedef struct { uint32_t var; } myObject;
myObject x, y;
...
y = x + 3; // syntax error.

This may seem contrived, but it is occasionally very useful.

like image 158
Stephen Canon Avatar answered Sep 28 '22 02:09

Stephen Canon