structure - Structures 1 Derived Data Type C gives you...

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

View Full Document Right Arrow Icon
1 Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 C gives you several ways to create a custom data type. The structure , which is a grouping of variables under one name and is called an aggregate data type. The “ typedef” keyword , allow us to define a new user-defined type . Derived Data Type
Background image of page 2
3 A structure is a collection of variables referenced under one name , providing a convenient means of keeping related information together. It allows us to organize related data of different types in one structure.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Structure Declaration A structure declaration forms a template that can be used to create structure objects (instances of a structure). The variables that make up the structure are called members . Usually, the members of a structure are logically related.
Background image of page 4
5 General Form struct tag { type member-name ; type member-name ; type member-name ; . . . } structure-variable(s) ;
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 struct addr { char name[30]; char street[40]; char city[20]; char state[3]; unsigned long int zip; }; Terminated by a semicolon keyword structure tag name No variable has actually been
Background image of page 6
7 Structure Variable Declaration struct addr addr_info; declares a variable of type struct addr called addr_info . The compiler automatically allocates sufficient memory to accommodate all of its members .
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Name 30 bytes Street 40 bytes City 20 bytes State 3 bytes Zip 4 bytes Memory allocated for addr_info
Background image of page 8
9 struct addr { char name[30]; char street[40]; char city[20]; char state[3]; unsigned long int zip; } addr_info, binfo, cinfo ; You can declare one or more objects when declare a struct type
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Initialization of Structures All, Extern and Static Variables including structure variables that are not explicitly initialized, are automatically initialized to 0 (appropriate value for that member’s type)
Background image of page 10
11 Accessing Structure Members Individual members of a structure are accessed through the use of the Dot Operator (“.”). General form : object-name . member-name
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 See slide 9 : Created 3 Struct vars . e.g., addr_info . zip = 12345; printf("%lu", addr_info . zip ); gets( addr_info . name ); for(t=0; addr_info . name[t]; ++t) putchar( addr_info . name[t] );
Background image of page 12
13 Structure Assignments #include <stdio.h> int main(void) { struct { int a; int b; } x, y; x.a = 10; y = x ; /* assign one structure to another */ printf("%d", y.a ); return 0; Do not need to assign the value of each member separately
Background image of page 13

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

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

Page1 / 43

structure - Structures 1 Derived Data Type C gives you...

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

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