Download presentation
Presentation is loading. Please wait.
Published byMikel Wilcott Modified over 9 years ago
1
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which are formed with the C built-in data types. To do this, we use the C structure. A structure is a composite data type defined by the user.
2
Structure Complex number is not part of the C data structure. But we can define a structure to represent complex numbers. Define a structure called complex which consists of a real field and imag field, both of them are doubles. struct complex { double real; double imag; } Declare three complex numbers: struct complex a, b, c; real and imag are called the member (or field) of the structure complex.
3
Structure Declaration Suppose that we want to represent an atomic element, including such features as its conventional name, scientific symbol, atomic weight, and mass. struct element { char name[10]; char symbol[5]; float aWgt; float mass; } Note that each field can be arbitrary data types, including structure.
4
Declare and Initialize a Structure Example: int n, ns[5]; float r, ts[4]; struct element e1, es[3]; strcpy( e1.name, "hydrogen"); strcpy( e1.symbol, "H"); e1.aWgt = 1.0; e1.mass = 3.0; To access each field of a structure, use the structure's name followed by a period '.' and the name of the field.
5
Structure Assignment struct element e1 = {"hydrogen", "H", 1.0, 3.0}; struct element es[3]; es[0] = e1; struct element f(...) {... return e1; } Structure as a whole can be used in assignment (as long as data type match), and returned from a function. However, any other operation must be on a field by field basis.
6
Structure, example #include main() { struct element { char name[10]; char symbol[5]; float aWgt; float mass; }; struct element es[3]; struct element e1 = {"Hydrogen", "H", 1.0, 3.0}; es[0] = e1; printf("%s, %s, %f, %f\n", es[0].name, e1.symbol, es[0].aWgt, e1.mass); }
7
Pointer to Structure typedef struct computer { char brand[7]; char *cpu; int ram_size; } E_Brain; E_Brain computer1, *ptr; ptr = &computer1; (*ptr).ram_size = 640; strcpy((*ptr).brand, "IBM"); (*ptr).cpu = "pentium";
8
Pointer Operator The following member access syntax forms are equivalent: (*ptr).cpu ptr -> cpu with: E_Brain computer1, *ptr; ptr = &computer1; we can also say ptr -> ram_size = 640; strcpy(ptr->brand, "IBM"); ptr -> cpu = "pentium";
9
Typedef Construct C typedef construct associates a built-in or user-defined type with a new (type) name. typedef oldname newname; struct pair { double real; double imaginary; }; typedef struct pair complex; typedef int* int_ptr; complex a b c; int_ptr p, q;
10
Reading/Home Working Read Chapter 10, page 490 to 525. Work on Problems –Section 10.1, page 496, exercise 1, 3, 5. –Section 10.2, page 503, exercise 1. –Section 10.5, page 517, exercise 1, 5, 11. Check your answers in the back of the textbook. Do not hand in.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.