1 / 23

Integer Types

Basic Data Types of C. Integer Types. short month; // half of a machine word int car; // one machine word unsigned long distance;. Character Types char c = ‘’; // one byte char EndOfLine = ‘<br>’;. Real Types double salary; // two machine words

xuan
Download Presentation

Integer Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Data Types of C 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

  2. Pointer Types – Initialization 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;

  3. Pointers – More 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 &pi; // type int** ; evaluates to the actual address of pi int **ppi = &pi ; // a pointer to a pointer to int. 2 pi pi ppi

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

  5. Array Types 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.

  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; left operand i -= ++j; j = j + 1; i = i – j; i *= j++; i = i * j; j = j + 1;

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

  10. Arrays vs Pointers The array identifier evaluates to the address of its first element. Its type is that of pointer to the element’s type. 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

  11. Traversal of an Array throughPointer Manipulation // iterate across an array without // knowing its actual size #include <iostream> 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 ); } #include <iostream> 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; } }

  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 voidputValues (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(intmatrix [ ][10],introwSize ); 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. new and delete Dynamic memory allocation: memory is allocated at run-time. (int) 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;

  16. Reference Type A reference serves as an alternative name for an object. Similar to the use of a pointer but without resorting to pointer syntax. 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

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

  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. 3 8 -4 6 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]. Initialization (1) int a[ ] = { 3, 8, -4, 6 }; b reference to a variable int &b = a[0];

  20. 3 8 -4 6 a[0] a[1] a[2] a[3] c is a pointer pointing at a[3]. 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. Initialization (2) address int *c = &(a[3]); b reference to a pointer int *&d = c; c d

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

  22. 3 8 -4 6 a[0] a[1] a[2] a[3] Update (1) 2 16 a[0]--; b--; b *d += b + a[1]; a[3] += a[0] + a[1]; c d e

  23. 2 8 -4 16 a[0] a[1] a[2] a[3] c now points at a[1]; so does d. Update (2) 12 26 52 c = &(a[1]); b **e -= a[2] ; a[1] -= a[2]; c d c[0] = *d + **e + b; a[1] = a[1] + a[1] + a[0]; e d[1] = 2 * (**e); a[2] = 2 * a[1];

More Related