Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.

Similar presentations


Presentation on theme: "Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char."— Presentation transcript:

1 Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char EndOfLine = ‘\n’; Real Types double salary; // two machine words float wage; // one machine word Basic Data Types of C

2 int *ip1, *ip2; double *dp; int ival = 1027; // pi initialized to address no object int *pi = 0; // pi2 initialized to address ival int *pi2 = &ival; // pi and pi2 now both address ival pi = pi2; // pi2 now address no object pi2 = 0; Pointer Types – Initialization

3 The void* pointer can hold the address value of any pointer type. int i = 0; int *pi = &i; double d = -9.01; double *pd = &d; void *pv = pi; // pv holds the address value of pi pv = pd; // pv now holds the address value of pd pi; // type int*; evaluates to the address contained within pi π // type int** ; evaluates to the actual address of pi int **ppi = &pi ; // a pointer to a pointer to int. pi ppi 2 Pointers – More

4 Pointer Arithmetic double *p; loc 0loc k pp+k sizeof (double) Data type Current address New address char int double p = 5000 p + 1 = p – 6 = 5000 + 1*2 = 5002 5001 5000 – 6*4 = 4976

5 double x[50], y[200]; double z[10][20]; // 2-dimensional array x[0]x[49] addr addr + 49 * 4 const int ArraySize = 10; long A[ArraySize]; int A[20]; int v = 20; A[v] = 0; // index v exceeds 19; core dumped. Array Types

6 Increment & Decrement Operators int ia[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int i = 0, j = 9; while ( i < 10) ia [i++] = ia[j--]; // ia[10] = {9, 8, 7, 6, 5, 5, 6, 7, 8, 9} i++ increments i after its current value is used as index into ia. int ia[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int i = -1, j = 10; while ( i < 9) // 9 instead of 10 because ia[10] is illegal. ia [++i] = ia[--j]; // ia[10] = {9, 8, 7, 6, 5, 5, 6, 7, 8, 9} ++i increments i before its use as an index into ia.

7 Shorthand Binary Operators i += 1;i = i + 1; i -= ++j;j = j + 1; i = i – j; i *= j++;i = i * j; j = j + 1; left operand

8 Multidimensional Arrays int ia[4][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} }; int main() { const int rowSize = 4; const int colSize = 3; int ia[ rowSize ] [ colSize ]; for ( int i = 0; i < rowSize; ++i) for ( int j = 0; j < colSize; ++j) ia [i][j] = i + j; }

9 Returns the size of an object or type name in bytes. sizeof (type name); sizeof (object); sizeof object ; #include int ia[] = { 0, 1, 2 }; size_t array_size = size_of ia; size_t element_num = array_size / sizeof ( int ); The value returned is of a machine-specific type size_t. The sizeof Operator

10 Arrays vs Pointers The array identifier evaluates to the address of its first element. int ia[ ] = { 0, 1, 2, 3, 5, 8, 13, 21 }; // The type of ia is int * // two equivalent forms ia; &ia[0]; // both yield the value of the first element *ia; ia[0]; // both acccess the address of // the second element &ia[1]; ia+1; // both access the value of // the second element *(ia+1); ia[1]; // the dereference operator has a // higher precedence than the // addition operator *ia + 1; // the sum of the first // element and 1 *(ia+1); // the second element Its type is that of pointer to the element’s type.

11 Traversal of an Array through Pointer Manipulation #include int main() { int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; int *pbegin = ia; int *pend = ia + 9; while (pbegin != pend ) { cout << *pbegin << ‘ ’; ++pbegin; } // iterate across an array without // knowing its actual size #include void ia_print (int *pbegin, int *pend) { while ( pbegin != pend) { cout << *pbegin << ‘ ’; ++pbegin; } int main() { int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; ia_print ( ia, ia+9 ); }

12 Array Parameters An array is always passed as a pointer to its first element. // three equivalent declarations of putValues( ) void putValues ( int* ); void putValues ( int[ ] ); // a better declaration if the argument // calling the function with is an array void putValues ( int [10] ); // ok, but the array’s size is irrelevant The changes to an array parameter within the called function are made to the array argument itself (call-by-reference). // A function does not intend to change the array elements void putValues (const int [ 10 ]);

13 Multidimensional Arrays as Parameters Specify the size of all its dimensions beyond that of its first. void putValues (int matrix [ ][10], int rowSize ); If the function is to accept a matrix with variable sizes in all its dimensions, it needs to use pointer to pointer types. void putValues (int matrix[ ][ ], int rowSize, int colSize); // wrong void putValues (int **matrix, int rowSize, int colSize); // ok now

14 Cont’d void putValues (int **matrix, int rowSize, int colSize); int ia[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; // illegal: the type of ia does not match int** putValues ( ia, 2, 3); int** pa; pa = new int* [ 2]; // two-step dynamic memory allocation for (int i = 0; i < 2; i++) pa[i] = new int [3]; pa[0][0] = 0; pa[0][1] = 1; pa[0][2] = 2; pa[1][0] = 3; pa[1][1] = 4; pa[1][2] = 5; putValues ( pa, 2, 3 ); // ok now

15 Dynamic memory allocation: memory is allocated at run-time. int *pi = new int; // pi → int *pi = new int (1024); // allocates and initializes an object int *pia = new int [10]; // allocates an array of ten integers delete pi; delete [ ] pia; (int) new and delete

16 Reference Type A reference serves as an alternative name for an object. int ival = 1024; int &refVal = ival; // ok: refVal is a reference to ival int &refVal; // error: a reference must be initialized to an object refVal += 2; // ok int *pi = &refVal; // initialize pi with the address of ival Similar to the use of a pointer but without resorting to pointer syntax.

17 References (Cont’d) // defines two objects of type int int ival = 1024, ival2 = 2048; // defines one reference and one object int &rval = ival, rval2 = ival2; // defines one object, one pointer, one reference int ival3 = 1024, *pi = &ival3, &ri = ival3; // defines two references int &rval3 = ival3, &rval4 = ival2; 1024 2048 ival ival2 rval 2048 rval2 ival3 pi rval3 rval4 ri

18 Pointers, Arrays, and References int a[ ] = { 3, 8, -4, 6 }; int &b = a[0]; int *c = &(a[3]); int *&d = c; int **e = &d; What are stored in the array a[ ] after execution of the statements below? b--; *d += b + a[1]; c = &(a[1]); **e -= a[2]; c[0] = *d + **e + b; d[1] = 2 * (**e);

19 Initialization (1) int a[ ] = { 3, 8, -4, 6 }; int &b = a[0]; 38-46 a[0] a[1] a[2] a[3] b is an alias for a[0]. It can be seen as another “variable” sharing the memory location assigned to a[0]. b reference to a variable

20 Initialization (2) int *c = &(a[3]); int *&d = c; 38-46 a[0] a[1] a[2] a[3] b c is a pointer pointing at a[3]. c d is a reference (different name) for the memory location named c. Whenever c changes its value, that is, points to a new location, d automatically changes its value and points to the same new location. d reference to a pointer address

21 Initialization (3) int **e = &d; 38-46 a[0] a[1] a[2] a[3] b e is a pointer pointing at the memory location labeled by d, i.e., by c. cd e address e is a pointer to a pointer.

22 Update (1) b--; 38-46 a[0] a[1] a[2] a[3] b cd e *d += b + a[1]; a[0]--; 216 a[3] += a[0] + a[1];

23 Update (2) c = &(a[1]); 28-416 a[0] a[1] a[2] a[3] b cd e **e -= a[2] ; a[1] -= a[2]; c now points at a[1]; so does d. 5212 c[0] = *d + **e + b; a[1] = a[1] + a[1] + a[0]; 26 d[1] = 2 * (**e); a[2] = 2 * a[1];


Download ppt "Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char."

Similar presentations


Ads by Google