Ch. 12: Structures, Enumerations, Unions, and Bit Fields

Ch. 12: Structures, Enumerations, Unions, and Bit Fields -...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Structures, Enumerations, Unions, and Bit Fields Outline: Structure Definition Declaration of Structure type Pointer to Structures Accessing Structure Members Structure Initialization Array of Structures Typedef for Structure Type Outline: (Cont.) Size of Structures Assigning Structures Passing Structures as Function Arguments Function Returning Structures Function Returning a Pointer to a Structure Dynamic Allocation of Memory for Structures Nested Structures A Sample Problem The offsetof Macro Outline: (Cont.) Alignment of Structures Enumerations Unions Bit-fields Structure Definition A structure is a collection of members that can be of different data types. Example: struct Student { int id; char name[32]; }; The keyword struct introduces the definition for structure Student . Student is the structure name (or tag of the structure) and is used to declare variables of the structure type Student contains two members of type int and array of char. The two members id and name are used to hold the student id and student name. Memory Storage for struct Student Declaration of Structure type The syntax of a structure declaration can be fairly complex. The common method to declare structures are presented below. 1) Declaring a tag name and then using the tag name to declare actual variables. Example: struct Student { int id; char name[32]; }; ... struct Student s; s is a variable of struct Student type with two members id and name . 2) Declaring a structure without using a tag name. struct { int id; char name[32]; } s; This is useful if the structure is used only in one place. 3) Declaring a structure with a tag name and variables. struct Student { int id; char name[32]; } s1, s2; Both s1 and s2 are variables of struct Student type. Pointer to Structures Declaring a pointer to structure is the same as declaring pointers to other objects. struct Student { int id; char name[32]; } *sp1; struct Student *sp2, **spp3; Both sp1 and sp2 are pointers to struct Student type. spp3 is a pointer to pointer to struct Student. Accessing Structure Members There are two methods to access structure members. The first method is to access a structure member by structure name. Method 1 . Using structure variable name, dot operator ( . ) and the name of the member field. struct Student s; s.id = 101; Example: (Accessing structure members by structure name) /* File: accessmember.c */ #include <stdio.h> #include <string.h> struct Student { int id; char name[32]; }; int main() { struct Student s; s.id = 101; strcpy(s.name, "John"); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); printf(Please input id and name\n); scanf(%d, &s.id); scanf(%s, s.name); printf("s.id = %d\n", s.id); printf("s.name = %s\n", s.name); return 0; } > acccessmember.c s.id = 101 s.name = John Please input id and name 102 Doe s.id = 102 s.name = Doe Accessing Structure Members (Cont.) Method 2....
View Full Document

Page1 / 66

Ch. 12: Structures, Enumerations, Unions, and Bit Fields -...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online