Você está na página 1de 9

Members are accessed using the name of the instance of a structure or union, a period (.

),
and the name of the member. For example, given the declaration of t from above, the
member known as y (of type float) can be accessed using the following syntax:

t.y

Structures are commonly accessed through pointers. Consider the following example that
defines a pointer to t, known as ptr_to_t:

struct s *ptr_to_t = &t;

Member y of t can then be accessed by dereferencing ptr_to_t and using the result as the
left operand:

(*ptr_to_t).y

Which is identical to the simpler t.y above as long as ptr_to_t points to t. Because this
operation is common, C provides an abbreviated syntax for accessing a member directly
from a pointer. With this syntax, the name of the instance is replaced with the name of
the pointer and the period is replaced with the character sequence ->. Thus, the following

method of accessing y is identical to the previous two:

ptr_to_t->y

Members of unions are accessed in the same way.


[edit] Initialization

A structure can be initialized in its declarations using an initializer list, similar to arrays.
If a structure is not initialized, the values of its members are undefined until assigned.
The components of the initializer list must agree, in type and number, with the
components of the structure itself.

The following statement will initialize a new instance of the structure s from above
known as pi:

struct s pi = { 3, 3.1415, "Pi" };

Designated initializers allow members to be initialized by name. The following


initialization is equivalent to the previous one.

struct s pi = { .x = 3, .y = 3.1415, .z = "Pi" };


Members may be initialized in any order, and those that are not explicitly mentioned are
set to zero.

Any one member of a union may be initialized using designated initializers.

union u value = { .y = 3.1415 };

In C89, a union could only be initialized with a value of the type of its first member. That
is, the union u from above can only be initialized with a value of type int.

union u value = { 3 };

[edit] Assignment

Assigning values to individual members of structures and unions is syntactically identical


to assigning values to any other object. The only difference is that the lvalue of the
assignment is the name of the member, as accessed by the syntax mentioned above.

A structure can also be assigned as a unit to another structure of the same type. Structures
(and pointers to structures) may also be used as function parameter and return types.

For example, the following statement assigns the value of 74 (the ASCII code point for
the letter 't') to the member named x in the structure t, from above:
t.x = 74;

And the same assignment, using ptr_to_t in place of t, would look like:

ptr_to_t->x = 74;

Assignment with members of unions is identical, except that each new assignment
changes the current type of the union, and the previous type and value are lost.
[edit] Other operations

According to the C standard, the only legal operations that can be performed on a
structure are copying it, assigning to it as a unit (or initializing it), taking its address with
the address-of (&) unary operator, and accessing its members. Unions have the same
restrictions. One of the operations implicitly forbidden is comparison: structures and
unions cannot be compared using C's standard comparison facilities (==, >, <, etc.).
[edit] Bit fields

C also provides a special type of structure member known as a bit field, which is an
integer with an explicitly specified number of bits. A bit field is declared as a structure
member of type int, signed int, unsigned int, or _Bool, following the member name by a
colon (:) and the number of bits it should occupy. The total number of bits in a single bit
field must not exceed the total number of bits in its declared type.
As a special exception to the usual C syntax rules, it is implementation-defined whether a
bit field declared as type int, without specifying signed or unsigned, is signed or
unsigned. Thus, it is recommended to explicitly specify signed or unsigned on all
structure members for portability.

Empty entries consisting of just a colon followed by a number of bits are also allowed;

these indicate padding.

The members of bit fields do not have addresses, and as such cannot be used with the
address-of (&) unary operator. The sizeof operator may not be applied to bit fields.

The following declaration declares a new structure type known as f and an instance of it
known as g. Comments provide a description of each of the members:

struct f
{
unsigned int flag : 1; /* a bi t flag: can eith

er be on (1) or off (0) */


signed int num : 4; /* a signed 4-bit field; range -7...7 or -8...7 */
: 3; /* 3 bits of padding to round out 8 bits */
} g;

[edit] Incomplete types

The body of a struct or union declaration, or a typedef thereof, may be omitted, yielding
an incomplete type. Such a type may not be instantiated (its size is not known), nor may
its members be accessed (they, too, are unknown); however, the derived pointer type may
be used (but not dereferenced).
Incomplete types are used to implement recursive structures; the body of the type
declaration may be deferred to later in the translation unit:

typedef struct Bert Bert;


typedef struct Wilma Wilma;

struct Bert
{
Wilma *wilma;
};

struct Wilma
{
Bert *bert;
};

Incomplete types are also used for data hiding; the incomplete type is defined in a header

file, and the body only within the relevant source file.
[edit] Operators

Main article: Operators in C and C++


[edit] Control structures

C is a free-form language.
Bracing style varies from programmer to programmer and can be the subject of debate.
See Indent style for more details.
[edit] Compound statements

In the items in this section, any <statement> can be replaced with a compound statement.
Compound statements have the form:

{
<optional-declaration-list>
<optional-statement-list>

and are used as the body of a function or anywhere that a single statement is expected.
The declaration-list declares variables to be used in that scope, and the statement-list are
the actions to be performed. Brackets define their own scope, and variables defined inside
those brackets will be automatically deallocated at the closing bracket. Declarations and
statements can be freely intermixed within a compound statement (as in C++).
[edit] Selection statements

C has two types of selection statements: the if statement and the switch statement.

The if statement is in the form:

if (<expression>)
<statement1>
else
<statement2>

In the if statement, if the <expression> in parentheses is nonzero (true), control passes to


<statement1>. If the else clause is present and the <expression> is zero (false), control
will pass to <statement2>. The "else <statement2> part is optional, and if absent, a false
<expression> will simply result in skipping over the <statement1>. An else always
matches the nearest previous unmatched if; braces may be used to override this when
necessary, or for clarity.

The switch statement causes control to be transferred to one of several statements


depending on the value of an expression, which must have integral type. The
substatement controlled by a switch is typically compound. Any statement within the
substatement may be labeled with one or more case labels, which consist of the keyword
case followed by a constant expression and then a colon (:). The syntax is as follows:
switch (<expression>)
{
case <label1> :
<statements 1>
case <label2> :
<statements 2>
break;
default :
<statements 3>
}

No two of the case constants associated with the same switch may have the same value.
There may be at most one default label associated with a switch - if none of the case
labels are equal to the expression in the parentheses following switch, control passes to
the default label, or if there is no default label, execution resumes just be

Você também pode gostar