C Data Types simple integral enum structured floating








![Using a Pointer to Access the Elements of a String char msg[ ] = Using a Pointer to Access the Elements of a String char msg[ ] =](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-9.jpg)
![int String. Length ( /* in */ const char str[ ] ) // - int String. Length ( /* in */ const char str[ ] ) // -](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-10.jpg)
![Operator new Syntax new Data. Type [Int. Expression] If memory is available, in an Operator new Syntax new Data. Type [Int. Expression] If memory is available, in an](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-11.jpg)



















- Slides: 30

C++ Data Types simple integral enum structured floating array struct union class char short int long bool float double long double address pointer reference 1

Addresses in Memory l when a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable int float char 2000 x x; number; ch; 2002 number 2006 ch 2

Obtaining Memory Addresses l the address of a non-array variable can be obtained by using the address-of operator & int float char x; number; ch; cout << “Address of x is “ << &x << endl; cout << “Address of number is “ << &number << endl; cout << “Address of ch is “ << &ch << endl; 3

What is a pointer variable? l A pointer variable is a variable whose value is the address of a location in memory. l to declare a pointer variable, you must specify the type of value that the pointer will point to, for example, int* ptr; // ptr will hold the address of an int char* q; // q will hold the address of a char 4

Using a Pointer Variable 2000 int x; x = 12; 12 x int* ptr; ptr = &x; 3000 2000 ptr NOTE: Because ptr holds the address of x, we say that ptr “points to” x 5

Unary operator * is the indirection (dereference) operator 2000 int x; x = 12; 12 x 3000 int* ptr; ptr = &x; cout << 2000 ptr *ptr; NOTE: The value pointed to by ptr is denoted by *ptr 6

Using the Dereference Operator 2000 int x; x = 12; 12 5 x int* ptr; ptr = &x; *ptr = 5; cout<<x; 3000 2000 ptr // changes the value // at address ptr to 5 7

Another Example char ch = 4000 ch; ‘A’; char* q; q = &ch; A Z ch 5000 6000 4000 *q = ‘Z’; char* p; p = q; q 4000 p // the rhs has value 4000 // now p and q both point to ch 8
![Using a Pointer to Access the Elements of a String char msg Using a Pointer to Access the Elements of a String char msg[ ] =](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-9.jpg)
Using a Pointer to Access the Elements of a String char msg[ ] = “Hello”; char* ptr; 3000 ‘M’ ‘a’ ‘H’ ‘e’ ‘l’ ‘o’ ‘ ’ 3001 3000 ptr = msg; // recall that msg == &msg[ 0 ] *ptr = ‘M’ ; ptr++; *ptr = ‘a’; // increments the address in ptr 9
![int String Length in const char str int String. Length ( /* in */ const char str[ ] ) // -](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-10.jpg)
int String. Length ( /* in */ const char str[ ] ) // - - - - - - - - - - - // Precondition: str is a null-terminated string // Postcondition: FCTVAL == length of str (not counting ‘ ’) // - - - - - - - - - - - { char* p ; int count = 0; p = str; while ( *p != ‘ ’ ) { count++ ; p++ ; // increments the address p by sizeof char } return count; } 10 10
![Operator new Syntax new Data Type Int Expression If memory is available in an Operator new Syntax new Data. Type [Int. Expression] If memory is available, in an](https://slidetodoc.com/presentation_image_h2/a02b14bd4b67516d6106f4cdac384596/image-11.jpg)
Operator new Syntax new Data. Type [Int. Expression] If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, program terminates with error message. The dynamically allocated object exists until the delete operator destroys it. 11

The NULL Pointer There is a pointer constant 0 called the “null pointer” denoted by NULL in header file cstddef. But NULL is not memory address 0. NOTE: It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer’s job to check for this. while (ptr != NULL) {. . . // ok to use *ptr here } 12

3 Kinds of Program Data l STATIC DATA: memory allocation exists throughout execution of program static long current. Seed; l AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function l DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete 13

Allocation of Memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time. DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new. 14

Dynamically Allocated Data char* ptr; ptr = new char; 2000 3000 B ptr *ptr = ‘B’; int num = 4; float *grade; 3000 6000 4000 6000 grade = new float[num]; 15

Structured Data Type A structured data type is a type in which each value is a collection of component items. l the entire collection has a single name l each component can be accessed individually 16

one. Person 5000. id 2037581 . name “John Smith” . dept ‘B’ . hours 23. 6 17

Another. Person 6000. id 5281003 . name “Mary Jones” . dept ‘A’ . hour 35 18

struct Record // declares a struct data type { // does not allocate memory long id ; string name ; char dept ; // struct members float hour; }; Record one. Person; // declare variables of Record another. Person ; 19 19

struct type Declaration SYNTAX struct Type. Name { Member. List }; Member. List // does not allocate memory SYNTAX Data. Type Member. Name ; . . . 20

struct type Declaration The struct declaration names a type and names the members of the struct. It does not allocate memory for any variables of that type! You still need to declare your struct variables. 21

More about struct type declarations If the struct type declaration precedes all functions it will be visible throughout the rest of the file. If it is placed within a function, only that function can use it. It is common to place struct type declarations with Type. Names in a (. h) header file and #include that file. It is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a 22 structure member.

Accessing struct Members Dot ( period ) is the member selection operator. After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot. EXAMPLES one. Person. hour another. Person. name 23

Valid operations on a struct member depend only on its type one. Person. id = 7581; cin >> one. Person. hour; another. Person. name = “Adel”; 24

Examples of aggregate struct operations another. Person = one. Person ; print. Reocrd(one. Person); // assignment // value parameter Change. Hours(one. Person); // reference parameter another. Person = Get. Record( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE. 25. .

void print. Record( /* in */ Record one. Person) // Prints out values of all members { cout <<“ID # “ << one. Person. id<<endl <<“Name: ”<<one. Person. name<<endl <<“Dept: ”<<one. Person. dept<< endl <<“Hours: ”<<one. Person. hour<<endl; } 26 26

Passing a struct Type by Reference void Change. Hour( /* inout */ Record& one. Person, float today_hour ) { one. Person. hour += today_hour; } 27

Record Get. Record ( ) // Obtains all information from keyboard { Record this. Person ; cout<<“ Enter ID, Name, dept, hourn”; cin>>this. Person. id; cin>>this. Person. name; cin>>this. Person. dept; cin>>this. Person. hour; return this. Person ; } 28 28

struct Date. Type { int month ; int day ; int year ; }; // Assume 1. . 12 // Assume 1. . 31 // Assume 1900. . 2050 struct Statistics. Type { float fail. Rate ; Date. Type last. Serviced ; int down. Days ; }; // Date. Type is a struct type struct Machine. Rec { int id. Number ; string description ; Statistics. Type history ; Date. Type purchase. Date ; float cost ; }; // Statistics. Type is a struct type Machine. Rec machine ; 29 29

Abstraction l is the separation of the essential qualities of an object from the details of how it works or is composed l focuses on what, not how l is necessary for managing large, complex software projects 30