-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.texi
1877 lines (1506 loc) · 55.2 KB
/
types.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@c This is part of The GNU C Reference Manual
@c Copyright (C) 2007-2009 Free Software Foundation, Inc.
@c See the file gnu-c-manual.texi for copying conditions.
@c author:tjr@gnu.org et al
@node Data Types
@chapter Data Types
@cindex data types
@cindex types
@menu
* Primitive Types::
* Enumerations::
* Unions::
* Structures::
* Arrays::
* Pointers::
* Incomplete Types::
* Type Qualifiers::
* Storage Class Specifiers::
* Renaming Types::
@end menu
@c ----------------------------------------------------------------------------
@node Primitive Types
@section Primitive Data Types
@cindex primitive data types
@cindex data types, primitive
@cindex types, primitive
@menu
* Integer Types::
* Real Number Types::
* Complex Number Types::
@end menu
@node Integer Types
@subsection Integer Types
@cindex integer types
@cindex data types, integer
@cindex types, integer
The integer data types range in size from at least 8 bits to at least 32 bits.
The C99 standard extends this range to include integer sizes of at least 64 bits.
You should use integer types for storing whole number values (and the @code{char}
data type for storing characters). (Note that the sizes and ranges listed
for these types are minimums; depending on your computer platform, these sizes
and ranges may be larger.)
While the minimum ranges provide a natural ordering, the standard does
not require that any two types have a different range. For example,
it is common for @code{int} and @code{long} to have the same range.
The standard even allows @code{signed char} and @code{long} to have
the same range, though such platforms are very unusual.
@itemize @bullet
@item @code{signed char}
@cindex @code{signed char} data type
@*
The 8-bit @code{signed char} data type can hold integer values in
the range of @minus{}128 to 127.
@item @code{unsigned char}
@cindex @code{unsigned char} data type
@*
The 8-bit @code{unsigned char} data type can hold integer values in
the range of 0 to 255.
@item @code{char}
@cindex @code{char} data type
@*
Depending on your system, the @code{char} data type is defined as
having the same range as either the @code{signed char} or the @code{unsigned char}
data type (they are three distinct types, however). By convention,
you should use the @code{char} data type
specifically for storing ASCII characters (such as @code{`m'}), including escape
sequences (such as @code{`\n'}).
@item @code{short int}
@cindex @code{short int} data type
@*
The 16-bit @code{short int} data type can hold integer values in the
range of @minus{}32,768 to 32,767. You may also refer to this data type as
@code{short}, @code{signed short int}, or @code{signed short}.
@item @code{unsigned short int}
@cindex @code{unsigned short int} data type
@*
The 16-bit @code{unsigned short int} data type can hold integer values
in the range of 0 to 65,535. You may also refer to this data type
as @code{unsigned short}.
@item @code{int}
@cindex @code{int} data type
@*
The 32-bit @code{int} data type can hold integer values in the range
of @minus{}2,147,483,648 to 2,147,483,647. You may also refer to this data type
as @code{signed int} or @code{signed}.
@item @code{unsigned int}
@cindex @code{unsigned int} data type
@*
The 32-bit @code{unsigned int} data type can hold integer values in
the range of 0 to 4,294,967,295. You may also refer to this data type
simply as @code{unsigned}.
@item @code{long int}
@cindex @code{long int} data type
@*
The 32-bit @code{long int} data type can hold integer values in
the range of at least @minus{}2,147,483,648 to 2,147,483,647. (Depending on
your system, this data type might be 64-bit, in which case its range is
identical to that of the @code{long long int} data type.) You may also
refer to this data type as @code{long}, @code{signed long int},
or @code{signed long}.
@item @code{unsigned long int}
@cindex @code{unsigned long int} data type
@*
The 32-bit @code{unsigned long int} data type can hold integer values
in the range of at least 0 to 4,294,967,295. (Depending on your
system, this data type might be 64-bit, in which case its range is
identical to that of the @code{unsigned long long int} data type.) You may
also refer to this data type as @code{unsigned long}.
@item @code{long long int}
@cindex @code{long long int} data type
@*
The 64-bit @code{long long int} data type can hold integer values in
the range of @w{@minus{}9,223,372,036,854,775,808} to @w{9,223,372,036,854,775,807}. You
may also refer to this data type as @code{long long},
@code{signed long long int} or @code{signed long long}. This type is
not part of C89, but is both part of C99 and a GNU C extension.
@item @code{unsigned long long int}
@cindex @code{unsigned long long int} data type
@*
The 64-bit @code{unsigned long long int} data type can hold integer
values in the range of at least 0 to @w{18,446,744,073,709,551,615}. You may
also refer to this data type as @code{unsigned long long}. This type is
not part of C89, but is both part of C99 and a GNU C extension.
@end itemize
Here are some examples of declaring and defining integer variables:
@example
@group
int foo;
unsigned int bar = 42;
char quux = 'a';
@end group
@end example
@noindent
The first line declares an integer named @code{foo} but does not define
its value; it is left unintialized, and its value should not be assumed
to be anything in particular.
@node Real Number Types
@subsection Real Number Types
@cindex real number types
@cindex floating point types
@cindex data types, real number
@cindex data types, floating point
@cindex types, real number
@cindex types, floating point
There are three data types that represent fractional numbers. While the
sizes and ranges of these types are consistent across most computer systems
in use today, historically the sizes of these types varied from system to
system. As such, the minimum and maximum values are stored in macro definitions
in the library header file @code{float.h}. In this section, we include the
names of the macro definitions in place of their possible values; check your
system's @code{float.h} for specific numbers.
@itemize @bullet
@item @code{float}
@cindex @code{float} data type
@*
The @code{float} data type is the smallest of the three floating point
types, if they differ in size at all. Its minimum value is stored in
the @code{FLT_MIN}, and should be no greater than @code{1e-37}. Its maximum
value is stored in @code{FLT_MAX}, and should be no less than @code{1e37}.
@item @code{double}
@cindex @code{double} data type
@*
The @code{double} data type is at least as large as the @code{float}
type, and it may be larger. Its minimum value is stored in
@code{DBL_MIN}, and its maximum value is stored in @code{DBL_MAX}.
@item @code{long double}
@cindex @code{long double} data type
@*
The @code{long double} data type is at least as large as the
@code{float} type, and it may be larger. Its minimum value is stored in
@code{LDBL_MIN}, and its maximum value is stored in @code{LDBL_MAX}.
@end itemize
@comment --End of the floating point types
@noindent
All floating point data types are signed; trying to use @code{unsigned float},
for example, will cause a compile-time error.
Here are some examples of declaring and defining real number variables:
@example
@group
float foo;
double bar = 114.3943;
@end group
@end example
@noindent
The first line declares a float named @code{foo} but does not define
its value; it is left unintialized, and its value should not be assumed
to be anything in particular.
The real number types provided in C are of finite precision, and
accordingly, not all real numbers can be represented exactly.
Most computer systems that GCC compiles for use a binary
representation for real numbers, which is unable to precisely
represent numbers such as, for example, 4.2. For this reason, we
recommend that you consider not comparing real numbers for exact
equality with the @code{==} operator, but rather check that real
numbers are within an acceptable tolerance.
There are other more subtle implications of these imprecise
representations; for more details, see David Goldberg's paper
@cite{What Every Computer Scientist Should Know About Floating-Point
Arithmetic} and section 4.2.2 of Donald Knuth's @cite{The Art of
Computer Programming}.
@node Complex Number Types
@subsection Complex Number Types
@cindex complex number types
@cindex data types, complex number
@cindex types, complex number
GCC introduced some complex number types as an
extension to C89. Similar features were introduced in
C99@footnote{C++ also has complex number support, but it is
incompatible with the ISO C99 types}, but there were a number of
differences. We describe the standard complex number types first.
@subsubsection Standard Complex Number Types
Complex types were introduced in C99. There are three complex
types:
@itemize @asis
@item @code{float _Complex}
@item @code{double _Complex}
@item @code{long double _Complex}
@end itemize
The names here begin with an underscore and an uppercase letter in
order to avoid conflicts with existing programs' identifiers.
However, the C99 standard header file @code{<complex.h>} introduces
some macros which make using complex types easier.
@itemize @asis
@item @code{complex}
@*
Expands to @code{_Complex}. This allows a variable to be declared as
@code{double complex} which seems more natural.
@item @code{I}
@*
A constant of type @code{const float _Complex} having the value of the
imaginary unit normally referred to as @math{i}.
@end itemize
The @code{<complex.h>} header file also declares a number of functions
for performing computations on complex numbers, for example the
@code{creal} and @code{cimag} functions which respectively return
the real and imaginary parts of a @code{double complex} number. Other
functions are also provided, as shown in this example:
@example
#include <complex.h>
#include <stdio.h>
void example (void)
@{
complex double z = 1.0 + 3.0*I;
printf ("Phase is %f, modulus is %f\n", carg (z), cabs (z));
@}
@end example
@subsubsection GCC Extensions for Complex Number Types
GCC also introduced complex types as a GCC extension to C89, but the
spelling is different. The floating-point complex types in GCC's C89
extension are
@itemize @asis
@item @code{__complex__ float}
@item @code{__complex__ double}
@item @code{__complex__ long double}
@end itemize
GCC's extension provided for complex types other than floating-point ones,
allowing you to declare complex character types and complex integer
types; in fact @code{__complex__} can be used with any of the
primitive data types. We won't give you a complete list of all
possibilities, but here are some examples:
@itemize @bullet
@item @code{__complex__ float}
@*
The @code{__complex__ float} data type has two components: a real
part and an imaginary part, both of which are of the @code{float} data type.
@item @code{__complex__ int}
@*
The @code{__complex__ int} data type also has two components: a
real part and an imaginary part, both of which are of the @code{int} data
type.
@end itemize
@comment --End list of __complex__ types
To extract the real part of a complex-valued expression, use the keyword
@code{__real__}, followed by the expression. Likewise, use @code{__imag__}
to extract the imaginary part.
@example
@group
__complex__ float a = 4 + 3i;
float b = __real__ a; /* @r{@code{b} is now 4.} */
float c = __imag__ a; /* @r{@code{c} is now 3.} */
@end group
@end example
This example creates a complex floating point variable @code{a},
and defines its real part as 4 and its imaginary part as 3. Then, the
real part is assigned to the floating point variable @code{b}, and the
imaginary part is assigned to the floating point variable @code{c}.
@c ----------------------------------------------------------------------------
@node Enumerations
@section Enumerations
@cindex enumerations
@cindex types, enumeration
@cindex data types, enumeration
An enumeration is a custom data type used for storing constant integer
values and referring to them by names. By default, these values are
of type @code{signed int}; however, you can use the @code{-fshort-enums}
GCC compiler option to cause the smallest possible integer type to be
used instead. Both of these behaviors conform to the C89 standard,
but mixing the use of these options within the same program can
produce incompatibilities.
@menu
* Defining Enumerations::
* Declaring Enumerations::
@end menu
@node Defining Enumerations
@subsection Defining Enumerations
@cindex defining enumerations
@cindex enumerations, defining
You define an enumeration using the @code{enum} keyword, followed by
the name of the enumeration (this is optional), followed by a list of
constant names (separated by commas and enclosed in braces), and ending
with a semicolon.
@example
@group
enum fruit @{grape, cherry, lemon, kiwi@};
@end group
@end example
That example defines an enumeration, @code{fruit}, which contains four
constant integer values, @code{grape}, @code{cherry}, @code{lemon}, and
@code{kiwi}, whose values are, by default, 0, 1, 2, and 3, respectively.
You can also specify one or more of the values explicitly:
@example
@group
enum more_fruit @{banana = -17, apple, blueberry, mango@};
@end group
@end example
That example defines @code{banana} to be @minus{}17, and the remaining
values are incremented by 1: @code{apple} is @minus{}16,
@code{blueberry} is @minus{}15, and @code{mango} is -14. Unless
specified otherwise, an enumeration value is equal to one more than
the previous value (and the first value defaults to 0).
You can also refer to an enumeration value defined earlier in the same
enumeration:
@example
@group
enum yet_more_fruit @{kumquat, raspberry, peach,
plum = peach + 2@};
@end group
@end example
In that example, @code{kumquat} is 0, @code{raspberry} is 1,
@code{peach} is 2, and @code{plum} is 4.
You can't use the same name for an @code{enum} as a @code{struct} or
@code{union} in the same scope.
@node Declaring Enumerations
@subsection Declaring Enumerations
@cindex declaring enumerations
@cindex enumerations, declaring
You can declare variables of an enumeration type both when the
enumeration is defined and afterward. This example declares one
variable, named @code{my_fruit} of type @code{enum fruit}, all in
a single statement:
@example
@group
enum fruit @{banana, apple, blueberry, mango@} my_fruit;
@end group
@end example
@noindent
while this example declares the type and variable separately:
@example
@group
enum fruit @{banana, apple, blueberry, mango@};
enum fruit my_fruit;
@end group
@end example
(Of course, you couldn't declare it that way if you hadn't named the
enumeration.)
Although such variables are considered to be of an enumeration type, you
can assign them any value that you could assign to an @code{int} variable,
including values from other enumerations. Furthermore, any variable that
can be assigned an @code{int} value can be assigned a value from an
enumeration.
However, you cannot change the values in an enumeration once it has been
defined; they are constant values. For example, this won't work:
@example
@group
enum fruit @{banana, apple, blueberry, mango@};
banana = 15; /* @r{You can't do this!} */
@end group
@end example
Enumerations are useful in conjunction with the @code{switch}
statement, because the compiler can warn you if you have failed to
handle one of the enumeration values. Using the example above, if
your code handles @code{banana}, @code{apple} and @code{mango} only
but not @code{blueberry}, GCC can generate a warning.
@c ----------------------------------------------------------------------------
@node Unions
@section Unions
@cindex unions
@cindex types, union
@cindex data types, union
A union is a custom data type used for storing several variables in the
same memory space. Although you can access any of those variables at any
time, you should only read from one of them at a time---assigning a value to
one of them overwrites the values in the others.
@menu
* Defining Unions::
* Declaring Union Variables::
* Accessing Union Members::
* Size of Unions::
@end menu
@node Defining Unions
@subsection Defining Unions
@cindex defining unions
@cindex unions, defining
You define a union using the @code{union} keyword followed by
the declarations of the union's members, enclosed in
braces. You declare each member of a union just as you would
normally declare a variable---using the data type followed by one
or more variable names separated by commas, and ending with a
semicolon. Then end the union definition with a semicolon after
the closing brace.
You should also include a name for the union between the @code{union}
keyword and the opening brace. This is syntactically optional, but if
you leave it out, you can't refer to that union data type later on
(without a @code{typedef}, @pxref{The typedef Statement}).
Here is an example of defining a simple union for holding an integer
value and a floating point value:
@example
@group
union numbers
@{
int i;
float f;
@};
@end group
@end example
That defines a union named @code{numbers}, which contains two
members, @code{i} and @code{f}, which are of type @code{int} and
@code{float}, respectively.
@node Declaring Union Variables
@subsection Declaring Union Variables
@cindex declaring union variables
@cindex union variables, declaring
You can declare variables of a union type when both you initially
define the union and after the definition, provided you gave the
union type a name.
@menu
* Declaring Union Variables at Definition::
* Declaring Union Variables After Definition::
* Initializing Union Members::
@end menu
@node Declaring Union Variables at Definition
@subsubsection Declaring Union Variables at Definition
@cindex declaring union variables at definition
@cindex union variables, declaring at definition
You can declare variables of a union type when you define the
union type by putting the variable names after the closing
brace of the union definition, but before the final semicolon.
You can declare more than one such variable by separating the names
with commas.
@example
@group
union numbers
@{
int i;
float f;
@} first_number, second_number;
@end group
@end example
That example declares two variables of type @code{union numbers},
@code{first_number} and @code{second_number}.
@node Declaring Union Variables After Definition
@subsubsection Declaring Union Variables After Definition
@cindex declaring union variables after definition
@cindex union variables, declaring after definition
You can declare variables of a union type after you define the
union by using the @code{union} keyword and the name you
gave the union type, followed by one or more variable names
separated by commas.
@example
@group
union numbers
@{
int i;
float f;
@};
union numbers first_number, second_number;
@end group
@end example
That example declares two variables of type @code{union numbers},
@code{first_number} and @code{second_number}.
@node Initializing Union Members
@subsubsection Initializing Union Members
@cindex initializing union members
@cindex union members, initializing
You can initialize the first member of a union variable when you
declare it:
@example
@group
union numbers
@{
int i;
float f;
@};
union numbers first_number = @{ 5 @};
@end group
@end example
In that example, the @code{i} member of @code{first_number} gets the
value 5. The @code{f} member is left alone.
Another way to initialize a union member is to specify the name of the
member to initialize. This way, you can initialize whichever member
you want to, not just the first one. There are two methods that you can
use---either follow the member name with a colon, and then its value,
like this:
@example
@group
union numbers first_number = @{ f: 3.14159 @};
@end group
@end example
@noindent
or precede the member name with a period and assign a value
with the assignment operator, like this:
@example
@group
union numbers first_number = @{ .f = 3.14159 @};
@end group
@end example
You can also initialize a union member when you declare the union
variable during the definition:
@example
@group
union numbers
@{
int i;
float f;
@} first_number = @{ 5 @};
@end group
@end example
@node Accessing Union Members
@subsection Accessing Union Members
@cindex accessing union members
@cindex union members, accessing
You can access the members of a union variable using the member
access operator. You put the name of the union
variable on the left side of the operator, and the name of the
member on the right side.
@example
@group
union numbers
@{
int i;
float f;
@};
union numbers first_number;
first_number.i = 5;
first_number.f = 3.9;
@end group
@end example
Notice in that example that giving a value to the @code{f} member overrides
the value stored in the @code{i} member.
@c If a union member is accessed after a value has been stored in a
@c different member, the behavior is implementation-defined, but this
@c document doesn't specify the behavior. There is an exception
@c though: if the two members are structs and they have a common
@c initial sequence.
@node Size of Unions
@subsection Size of Unions
@cindex size of unions
@cindex unions, size of
This size of a union is equal to the size of its largest member. Consider
the first union example from this section:
@example
@group
union numbers
@{
int i;
float f;
@};
@end group
@end example
@noindent The size of the union data type is the same as @code{sizeof (float)},
because the @code{float} type is larger than the @code{int} type. Since all
of the members of a union occupy the same memory space, the union data type
size doesn't need to be large enough to hold the sum of all their sizes; it
just needs to be large enough to hold the largest member.
@c ----------------------------------------------------------------------------
@node Structures
@section Structures
@cindex structures
@cindex types, structure
@cindex data types, structure
A structure is a programmer-defined data type made up of
variables of other data types (possibly including other structure types).
@menu
* Defining Structures::
* Declaring Structure Variables::
* Accessing Structure Members::
* Bit Fields::
* Size of Structures::
@end menu
@node Defining Structures
@subsection Defining Structures
@cindex defining structures
@cindex structures, defining
You define a structure using the @code{struct} keyword followed by
the declarations of the structure's members, enclosed in
braces. You declare each member of a structure just as you would
normally declare a variable---using the data type followed by one
or more variable names separated by commas, and ending with a
semicolon. Then end the structure definition with a semicolon after
the closing brace.
You should also include a name for the structure in between the
@code{struct} keyword and the opening brace. This is optional, but if
you leave it out, you can't refer to that structure data type later
on (without a @code{typedef}, @pxref{The typedef Statement}).
Here is an example of defining a simple structure for holding the
X and Y coordinates of a point:
@example
@group
struct point
@{
int x, y;
@};
@end group
@end example
That defines a structure type named @code{struct point}, which contains two
members, @code{x} and @code{y}, both of which are of type @code{int}.
Structures (and unions) may contain instances of other structures and
unions, but of course not themselves. It is possible for a structure
or union type to contain a field which is a pointer to the same type
(@pxref{Incomplete Types}).
@node Declaring Structure Variables
@subsection Declaring Structure Variables
@cindex declaring structure variables
@cindex structure variables, declaring
You can declare variables of a structure type when both you initially
define the structure and after the definition, provided you gave the
structure type a name.
@menu
* Declaring Structure Variables at Definition::
* Declaring Structure Variables After Definition::
* Initializing Structure Members::
@end menu
@node Declaring Structure Variables at Definition
@subsubsection Declaring Structure Variables at Definition
@cindex declaring structure variables at definition
@cindex structure variables, declaring at definition
You can declare variables of a structure type when you define the
structure type by putting the variable names after the closing
brace of the structure definition, but before the final semicolon.
You can declare more than one such variable by separating the names
with commas.
@example
@group
struct point
@{
int x, y;
@} first_point, second_point;
@end group
@end example
That example declares two variables of type @code{struct point},
@code{first_point} and @code{second_point}.
@node Declaring Structure Variables After Definition
@subsubsection Declaring Structure Variables After Definition
@cindex declaring structure variables after definition
@cindex structure variables, declaring after definition
You can declare variables of a structure type after defining the
structure by using the @code{struct} keyword and the name you
gave the structure type, followed by one or more variable names
separated by commas.
@example
@group
struct point
@{
int x, y;
@};
struct point first_point, second_point;
@end group
@end example
That example declares two variables of type @code{struct point},
@code{first_point} and @code{second_point}.
@node Initializing Structure Members
@subsubsection Initializing Structure Members
@cindex initializing structure members
@cindex structure members, initializing
You can initialize the members of a structure type to have certain
values when you declare structure variables.
If you do not initialize a structure variable, the effect depends on
whether it is has static storage (@pxref{Storage Class Specifiers}) or
not. If it is, members with integral types are initialized with 0 and
pointer members are initialized to NULL; otherwise, the value of the
structure's members is indeterminate.
One way to initialize a structure is to specify the values in a set of
braces and separated by commas. Those values are assigned to the
structure members in the same order that the members are declared in
the structure in definition.
@example
@group
struct point
@{
int x, y;
@};
struct point first_point = @{ 5, 10 @};
@end group
@end example
In that example, the @code{x} member of @code{first_point} gets the
value 5, and the @code{y} member gets the value 10.
Another way to initialize the members is to specify the name of the
member to initialize. This way, you can initialize the members in
any order you like, and even leave some of them uninitialized. There
are two methods that you can use. The first method is available in
C99 and as a C89 extension in GCC:
@example
@group
struct point first_point = @{ .y = 10, .x = 5 @};
@end group
@end example
You can also omit the period and use a colon instead of @samp{=},
though this is a GNU C extension:
@example
@group
struct point first_point = @{ y: 10, x: 5 @};
@end group
@end example
You can also initialize the structure variable's members when you declare
the variable during the structure definition:
@example
@group
struct point
@{
int x, y;
@} first_point = @{ 5, 10 @};
@end group
@end example
You can also initialize fewer than all of a structure variable's members:
@example
@group
struct pointy
@{
int x, y;
char *p;
@};
struct pointy first_pointy = @{ 5 @};
@end group
@end example
Here, @code{x} is initialized with 5, @code{y} is initialized with 0,
and @code{p} is initialized with NULL. The rule here is that @code{y}
and @code{p} are initialized just as they would be if they were static
variables.
@c See ANSI C89, sec 6.5.7, ``Initialization''.
Here is another example that initializes a structure's members which
are structure variables themselves:
@example
@group
struct point
@{
int x, y;
@};
struct rectangle
@{
struct point top_left, bottom_right;
@};
struct rectangle my_rectangle = @{ @{0, 5@}, @{10, 0@} @};
@end group
@end example
That example defines the @code{rectangle} structure to consist of
two @code{point} structure variables. Then it declares one variable
of type @code{struct rectangle} and initializes its members. Since
its members are structure variables, we used an extra set of braces
surrounding the members that belong to the @code{point} structure
variables. However, those extra braces are not necessary; they just
make the code easier to read.
@node Accessing Structure Members
@subsection Accessing Structure Members
@cindex accessing structure members
@cindex structure members, accessing
You can access the members of a structure variable using the member
access operator. You put the name of the structure
variable on the left side of the operator, and the name of the
member on the right side.
@example
@group
struct point
@{
int x, y;
@};
struct point first_point;
first_point.x = 0;
first_point.y = 5;
@end group
@end example
You can also access the members of a structure variable which is itself a
member of a structure variable.
@example
@group
struct rectangle
@{
struct point top_left, bottom_right;
@};
struct rectangle my_rectangle;
my_rectangle.top_left.x = 0;
my_rectangle.top_left.y = 5;
my_rectangle.bottom_right.x = 10;
my_rectangle.bottom_right.y = 0;
@end group
@end example