Structure in C
Structure in C
C Structure
Introducing to C structure
In some programming contexts, you need to access multiple data types under a single name for easy manipulation; for example you want to refer to address with multiple data like house number, street, zip code, country C supports structure which allows you to wrap one or more variables with different data types. A structure can contain any valid data types like int, char, float even arrays and other structures. Each variable in structure is called a structure member.
Defining structure
To define a structure, you can use struct keyword. Here is the common syntax of structure definition: struct struct_name{ structure_member }; The name of structure is followed the rule of variable name. Here is an example of defining address structure: view source print?
1.struct address{ 2. unsigned int house_number; 3. char street_name[50]; 4. int zip_code; 5. char country[50]; 6. };
It contains house number as an positive integer, street name as a string, zip code as an integer and country as a string.
Declaring structure
The above example only defines an address structure without create any instance of it. To create or declare a structure you have two ways: The first way is declare a structure follows by structure definition like this : view source print?
1.struct struct_name { 2. structure_member;
3. 4.
In the second way you can declare your structure instance at a different location in your source code after structure definition. Here is structure declaration syntax : view source print?
1.struct struct_name instance_1,instance_2 instance_n;
Complex structure
Structure can contains arrays or other structures so it is sometimes called complex structure. For example address structure is a complex structure. We can define a complex structure which contains address structure as follows: view source print?
1.struct customer{ 2. char name[50]; 3. structure address billing_addr; 4. structure address shipping_addr; 5. };
If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure. view source print?
1.struct customer jack; 2.jack.billing_addr.country = "US";
Initializing structure
C programming language treats a structure as a custom data type therefore you can initialize a structure like a variable. Here is an example of initialize product structure: view source
print?
1.struct product{ 2. char name[50]; 3. double price; 4. } book = { "C programming language",40.5};
In above example, we define product structure, then we declare and initialize book structure with its name and price.
1.struct_intance1 = struct_intance2
be noted that some old C compilers may not supports structure assignment so you have to assign each member variables one by one.
You will never get the size of a structure exactly as you think it must be. The sizeof function returns the size of structure larger than it is because the compiler pads struct members so that each one can be accessed faster without delays. So you should be careful when you read the whole structure from file which were written from other programs.
08. 09. 10.void print_list(student list[], int size); 11.void read_list(student list[], int size); 12. 13. 14. 15.void main(){ 16. 17. const int size = 3; 18. student list[size]; 19. 20. read_list(list,size); 21. 22. print_list(list,size); 23. 24. 25.} 26. 27.void read_list(student list[], int size) 28.{ 29. printf("Please enter the student information:\n"); 30. 31. for(int i = 0; i < size;i++){ 32. printf("\nname:"); 33. scanf("%S",&list[i].name); 34. 35. printf("\nmark:"); 36. scanf("%U",&list[i].mark); 37. } 38. 39.} 40. 41.void print_list(student list[], int size){ 42. printf("Students' information:\n"); 43. 44. for(int i = 0; i < size;i++){ 45. printf("\nname: %s, mark: %u",list[i].name,list[i].mark); 46. } 47.}
name:Harry mark:8 Students' information: name: J, mark: 5 name: A, mark: 7 name: H, mark: 8
Structure Declarations
A "structure declaration" names a type and specifies a sequence of variable values (called "members" or "fields" of the structure) that can have different types. An optional identifier, called a "tag," gives the name of the structure type and can be used in subsequent references to the structure type. A variable of that structure type holds the entire sequence defined by that type. Structures in C are similar to the types known as "records" in other languages.
Syntax
struct-or-union-specifier:
struct union
struct-declaration-list:
specifier-qualifier-list struct-declarator-list ;
specifier-qualifier-list:
declarator The declaration of a structure type does not set aside space for a structure. It is only a template for later declarations of structure variables. A previously defined identifier (tag) can be used to refer to a structure type defined elsewhere. In this case, struct-declaration-list cannot be repeated as long as the definition is visible. Declarations of pointers to structures and typedefs for structure types can use the structure tag before the structure type is defined. However, the structure definition must be encountered prior to any actual use of the size of the fields. This is an incomplete definition of the type and the type tag. For this definition to be completed, a type definition must appear later in the same scope. The struct-declaration-list specifies the types and names of the structure members. A structdeclaration-list argument contains one or more variable or bit-field declarations. Each variable declared in struct-declaration-list is defined as a member of the structure type. Variable declarations within struct-declaration-list have the same form as other variable declarations discussed in this section, except that the declarations cannot contain storage-class specifiers or initializers. The structure members can have any variable types except type void, an incomplete type, or a function type. A member cannot be declared to have the type of the structure in which it appears. However, a member can be declared as a pointer to the structure type in which it appears as long as the structure type has a tag. This allows you to create linked lists of structures. Structures follow the same scoping as other identifiers. Structure identifiers must be distinct from other structure, union, and enumeration tags with the same visibility. Each struct-declaration in a struct-declaration-list must be unique within the list. However, identifier names in a struct-declaration-list do not have to be distinct from ordinary variable names or from identifiers in other structure declaration lists. Nested structures can also be accessed as though they were declared at the file-scope level. For example, given this declaration:
Examples
These examples illustrate structure declarations:
struct employee /* Defines a structure variable named temp */ { char name[20]; int id; long class; } temp;
The employee structure has three members: name, id, and class. The name member is a 20element array, and id and class are simple members with int and long type, respectively. The identifier employee is the structure identifier.
struct employee student, faculty, staff;
This example defines three structure variables: student, faculty, and staff. Each structure has the same list of three members. The members are declared to have the structure type employee, defined in the previous example.
struct { float x, y; } complex; /* Defines an anonymous struct and a */ /* structure variable named complex */
The complex structure has two members with float type, x and y. The structure type has no tag and is therefore unnamed or anonymous.
struct sample /* Defines a structure named x */ { char c; float *pf; struct sample *next; } x;
The first two members of the structure are a char variable and a pointer to a float value. The third member, next, is declared as a pointer to the structure type being defined (sample). Anonymous structures can be useful when the tag named is not needed. This is the case when one declaration defines all structure instances. For example:
struct { int x; int y; } mystruct;
Microsoft Specific The compiler allows an unsized or zero-sized array as the last member of a structure. This can be useful if the size of a constant array differs when used in various situations. The declaration of such a structure looks like this: struct identifier{ set-of-declarations type array-name[ ];};
Unsized arrays can appear only as the last member of a structure. Structures containing unsized array declarations can be nested within other structures as long as no further members are declared in any enclosing structures. Arrays of such structures are not allowed. The sizeof operator, when applied to a variable of this type or to the type itself, assumes 0 for the size of the array. Structure declarations can also be specified without a declarator when they are members of another structure or union. The field names are promoted into the enclosing structure. For example, a nameless structure looks like this:
struct s { float y; struct { int a, b, c; }; char str[10]; } *p_s;
. . . p_s->b = 100;
See Structure and Union Members for information about structure references. END Microsoft Specific
See Also