📄 extend.texi
字号:
@smallexamplefoo (float f, float g)@{ float beat_freqs[2] = @{ f-g, f+g @}; /* @r{@dots{}} */@}@end smallexample@node Compound Literals@section Compound Literals@cindex constructor expressions@cindex initializations in expressions@cindex structures, constructor expression@cindex expressions, constructor@cindex compound literals@c The GNU C name for what C99 calls compound literals was "constructor expressions".ISO C99 supports compound literals. A compound literal looks likea cast containing an initializer. Its value is an object of thetype specified in the cast, containing the elements specified inthe initializer; it is an lvalue. As an extension, GCC supportscompound literals in C89 mode and in C++.Usually, the specified type is a structure. Assume that@code{struct foo} and @code{structure} are declared as shown:@smallexamplestruct foo @{int a; char b[2];@} structure;@end smallexample@noindentHere is an example of constructing a @code{struct foo} with a compound literal:@smallexamplestructure = ((struct foo) @{x + y, 'a', 0@});@end smallexample@noindentThis is equivalent to writing the following:@smallexample@{ struct foo temp = @{x + y, 'a', 0@}; structure = temp;@}@end smallexampleYou can also construct an array. If all the elements of the compound literalare (made up of) simple constant expressions, suitable for use ininitializers of objects of static storage duration, then the compoundliteral can be coerced to a pointer to its first element and used insuch an initializer, as shown here:@smallexamplechar **foo = (char *[]) @{ "x", "y", "z" @};@end smallexampleCompound literals for scalar types and union types are isalso allowed, but then the compound literal is equivalentto a cast.As a GNU extension, GCC allows initialization of objects with static storageduration by compound literals (which is not possible in ISO C99, becausethe initializer is not a constant).It is handled as if the object was initialized only with the bracketenclosed list if the types of the compound literal and the object match.The initializer list of the compound literal must be constant.If the object being initialized has array type of unknown size, the size isdetermined by compound literal size.@smallexamplestatic struct foo x = (struct foo) @{1, 'a', 'b'@};static int y[] = (int []) @{1, 2, 3@};static int z[] = (int [3]) @{1@};@end smallexample@noindentThe above lines are equivalent to the following:@smallexamplestatic struct foo x = @{1, 'a', 'b'@};static int y[] = @{1, 2, 3@};static int z[] = @{1, 0, 0@};@end smallexample@node Designated Inits@section Designated Initializers@cindex initializers with labeled elements@cindex labeled elements in initializers@cindex case labels in initializers@cindex designated initializersStandard C89 requires the elements of an initializer to appear in a fixedorder, the same as the order of the elements in the array or structurebeing initialized.In ISO C99 you can give the elements in any order, specifying the arrayindices or structure field names they apply to, and GNU C allows this asan extension in C89 mode as well. This extension is notimplemented in GNU C++.To specify an array index, write@samp{[@var{index}] =} before the element value. For example,@smallexampleint a[6] = @{ [4] = 29, [2] = 15 @};@end smallexample@noindentis equivalent to@smallexampleint a[6] = @{ 0, 0, 15, 0, 29, 0 @};@end smallexample@noindentThe index values must be constant expressions, even if the array beinginitialized is automatic.An alternative syntax for this which has been obsolete since GCC 2.5 butGCC still accepts is to write @samp{[@var{index}]} before the elementvalue, with no @samp{=}.To initialize a range of elements to the same value, write@samp{[@var{first} ... @var{last}] = @var{value}}. This is a GNUextension. For example,@smallexampleint widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @};@end smallexample@noindentIf the value in it has side-effects, the side-effects will happen only once,not for each initialized field by the range initializer.@noindentNote that the length of the array is the highest value specifiedplus one.In a structure initializer, specify the name of a field to initializewith @samp{.@var{fieldname} =} before the element value. For example,given the following structure,@smallexamplestruct point @{ int x, y; @};@end smallexample@noindentthe following initialization@smallexamplestruct point p = @{ .y = yvalue, .x = xvalue @};@end smallexample@noindentis equivalent to@smallexamplestruct point p = @{ xvalue, yvalue @};@end smallexampleAnother syntax which has the same meaning, obsolete since GCC 2.5, is@samp{@var{fieldname}:}, as shown here:@smallexamplestruct point p = @{ y: yvalue, x: xvalue @};@end smallexample@cindex designatorsThe @samp{[@var{index}]} or @samp{.@var{fieldname}} is known as a@dfn{designator}. You can also use a designator (or the obsolete colonsyntax) when initializing a union, to specify which element of the unionshould be used. For example,@smallexampleunion foo @{ int i; double d; @};union foo f = @{ .d = 4 @};@end smallexample@noindentwill convert 4 to a @code{double} to store it in the union usingthe second element. By contrast, casting 4 to type @code{union foo}would store it into the union as the integer @code{i}, since it isan integer. (@xref{Cast to Union}.)You can combine this technique of naming elements with ordinary Cinitialization of successive elements. Each initializer element thatdoes not have a designator applies to the next consecutive element of thearray or structure. For example,@smallexampleint a[6] = @{ [1] = v1, v2, [4] = v4 @};@end smallexample@noindentis equivalent to@smallexampleint a[6] = @{ 0, v1, v2, 0, v4, 0 @};@end smallexampleLabeling the elements of an array initializer is especially usefulwhen the indices are characters or belong to an @code{enum} type.For example:@smallexampleint whitespace[256] = @{ [' '] = 1, ['\t'] = 1, ['\h'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @};@end smallexample@cindex designator listsYou can also write a series of @samp{.@var{fieldname}} and@samp{[@var{index}]} designators before an @samp{=} to specify anested subobject to initialize; the list is taken relative to thesubobject corresponding to the closest surrounding brace pair. Forexample, with the @samp{struct point} declaration above:@smallexamplestruct point ptarray[10] = @{ [2].y = yv2, [2].x = xv2, [0].x = xv0 @};@end smallexample@noindentIf the same field is initialized multiple times, it will have value fromthe last initialization. If any such overridden initialization hasside-effect, it is unspecified whether the side-effect happens or not.Currently, GCC will discard them and issue a warning.@node Case Ranges@section Case Ranges@cindex case ranges@cindex ranges in case statementsYou can specify a range of consecutive values in a single @code{case} label,like this:@smallexamplecase @var{low} ... @var{high}:@end smallexample@noindentThis has the same effect as the proper number of individual @code{case}labels, one for each integer value from @var{low} to @var{high}, inclusive.This feature is especially useful for ranges of ASCII character codes:@smallexamplecase 'A' ... 'Z':@end smallexample@strong{Be careful:} Write spaces around the @code{...}, for otherwiseit may be parsed wrong when you use it with integer values. For example,write this:@smallexamplecase 1 ... 5:@end smallexample@noindentrather than this:@smallexamplecase 1...5:@end smallexample@node Cast to Union@section Cast to a Union Type@cindex cast to a union@cindex union, casting to aA cast to union type is similar to other casts, except that the typespecified is a union type. You can specify the type either with@code{union @var{tag}} or with a typedef name. A cast to union is actuallya constructor though, not a cast, and hence does not yield an lvalue likenormal casts. (@xref{Compound Literals}.)The types that may be cast to the union type are those of the membersof the union. Thus, given the following union and variables:@smallexampleunion foo @{ int i; double d; @};int x;double y;@end smallexample@noindentboth @code{x} and @code{y} can be cast to type @code{union foo}.Using the cast as the right-hand side of an assignment to a variable ofunion type is equivalent to storing in a member of the union:@smallexampleunion foo u;/* @r{@dots{}} */u = (union foo) x @equiv{} u.i = xu = (union foo) y @equiv{} u.d = y@end smallexampleYou can also use the union cast as a function argument:@smallexamplevoid hack (union foo);/* @r{@dots{}} */hack ((union foo) x);@end smallexample@node Mixed Declarations@section Mixed Declarations and Code@cindex mixed declarations and code@cindex declarations, mixed with code@cindex code, mixed with declarationsISO C99 and ISO C++ allow declarations and code to be freely mixedwithin compound statements. As an extension, GCC also allows this inC89 mode. For example, you could do:@smallexampleint i;/* @r{@dots{}} */i++;int j = i + 2;@end smallexampleEach identifier is visible from where it is declared until the end ofthe enclosing block.@node Function Attributes@section Declaring Attributes of Functions@cindex function attributes@cindex declaring attributes of functions@cindex functions that never return@cindex functions that return more than once@cindex functions that have no side effects@cindex functions in arbitrary sections@cindex functions that behave like malloc@cindex @code{volatile} applied to function@cindex @code{const} applied to function@cindex functions with @code{printf}, @code{scanf}, @code{strftime} or @code{strfmon} style arguments@cindex functions with non-null pointer arguments@cindex functions that are passed arguments in registers on the 386@cindex functions that pop the argument stack on the 386@cindex functions that do not pop the argument stack on the 386In GNU C, you declare certain things about functions called in your programwhich help the compiler optimize function calls and check your code morecarefully.The keyword @code{__attribute__} allows you to specify specialattributes when making a declaration. This keyword is followed by anattribute specification inside double parentheses. The followingattributes are currently defined for functions on all targets:@code{aligned}, @code{alloc_size}, @code{noreturn},@code{returns_twice}, @code{noinline}, @code{always_inline},@code{flatten}, @code{pure}, @code{const}, @code{nothrow},@code{sentinel}, @code{format}, @code{format_arg},@code{no_instrument_function}, @code{section}, @code{constructor},@code{destructor}, @code{used}, @code{unused}, @code{deprecated},@code{weak}, @code{malloc}, @code{alias}, @code{warn_unused_result},@code{nonnull}, @code{gnu_inline}, @code{externally_visible},@code{hot}, @code{cold}, @code{artificial}, @code{error}and @code{warning}.Several other attributes are defined for functions
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -