Download presentation
Presentation is loading. Please wait.
Published byDiana Shaw Modified over 9 years ago
1
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training
2
Training C/C++EcoSoftware 2 Structure Data o Introduction Structure data. o Defining a Structure. o Declaring Structure Variables. o Accessing Structure Members o Initializing Structures. o Assignment Statements with structure o Array of Structures. o Pointers to Structures o Union
3
Training C/C++EcoSoftware 3 Introduction Structure data. o A structure consists of a number of data items, which need not be of the same data type, grouped together. o The structure could hold as many of these items as desired. 1 Variable ILLUSIONBACH1 Name of the bookAuthor Edition I L L U S I O N Array
4
Training C/C++EcoSoftware 4 Defining a Structure o A structure definition forms a template for creating structure variables o The variables in the structure are called structure elements or structure members o Example: … … 2000 2001 2002 2003 2004 2029 2030 2031 2032 2033 number name on_hand
5
Training C/C++EcoSoftware 5 Declaring Structure Variables o Once the structure has been defined, one or more variables of that type can be declared o Example: struct cat book; o The statement sets aside enough memory to hold all items in the structure struct cat books1, books2; or struct cat books1; struct cat books2; struct cat { char bk_name[25]; char author[20]; float price; } books1, books2;
6
Training C/C++EcoSoftware 6 Accessing Structure Members Structure elements are referenced through the use of the dot operator (.), also known as the membership operator Syntax: structure_name.element_name Example: scanf(“%s”, books1.bk_name);
7
Training C/C++EcoSoftware 7 Initializing Structures o Like variables and arrays, structure variables can be initialized at the point of declaration struct employee {int no; char name [20]; }; o Variables emp1 and emp2 of the type employee can be declared and initialized as: struct employee emp1 = {346, “Abraham”}; struct employee emp2 = {347, “John”};
8
Training C/C++EcoSoftware 8 Assignment Statements Used with Structures-1 o It is possible to assign the values of one structure variable to another variable of the same type using a simple assignment statement o For example : if books 1 and books2 are structure variables of the same type, the following statement is valid books2 = books1;
9
Training C/C++EcoSoftware 9 Assignment Statements Used with Structures - 2 o In cases where direct assignment is not possible, the in- built function memcpy() can be used o Syntax: memcpy (char * destn, char &source, int nbytes); o Example: memcpy (&books2, &books1, sizeof(struct cat));
10
Training C/C++EcoSoftware 10 Structures within Structures It is possible to have one structure within another structure. A structure cannot be nested within itself To access the elements of the structure the format will be similar to the one used with normal structures, struct issue { char borrower [20]; char dt_of_issue[8]; struct cat books; }issl; issl.borrower To access elements of the structure cat, which is a part of another structure issue, issl.books.author
11
Training C/C++EcoSoftware 11 Passing Structures as Arguments o A structure variable can be passed as an argument to a function o This facility is used to pass groups of logically related data items together instead of passing them one by one o The type of the argument should match the type of the parameter
12
Training C/C++EcoSoftware 12 Array of Structures o A common use of structures is in arrays of structures o A structure is first defined, and then an array variable of that type is declared o Example: struct cat books[50]; o To the access the variable author of the fourth element of the array books: books[4].author
13
Training C/C++EcoSoftware 13 Initialization of Structure Arrays o Structure arrays are initialized by enclosing the list of values of its elements within a pair of braces o Example: struct unit { char ch; int i; }; struct unit series [3] = {{‘a’, 100} {‘b’, 200} {‘c’, 300} };
14
Training C/C++EcoSoftware 14 Pointers to Structures o Structure pointers are declared by placing an asterisk(*) in front of the structure variable’s name o The -> operator is used to access the elements of a structure using a pointer o Example: struct cat *ptr_bk; ptr_bk = &books; printf(“%s”, ptr_bk->author); o Structure pointers passed as arguments to functions enable the function to modify the structure elements directly
15
Training C/C++EcoSoftware 15 The typedef keyword o A new data type name can be defined by using the keyword typedef o It does not create a new data type, but defines a new name for an existing type o Syntax: typedef type name; o Example: typedef float deci; o typedef cannot be used with storage classes
16
Training C/C++EcoSoftware 16 Union o Allows the same memory area to be shared by a number of different variables. o Syntax Declaring a union is similar to that used for structures union cat books1, books2; or union cat books1; union cat books2; union cat { char bk_name[25]; char author[20]; float price; } books1, books2;
17
Training C/C++EcoSoftware 17 Example Union
18
Training C/C++EcoSoftware 18 Pointers to Unions o define a pointer to a union with a statement such as : union u_example *pU; pU = &U2; U1.decval = pU->decval;
19
Training C/C++EcoSoftware 19 Thank You End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.