Upload
charla-richardson
View
226
Download
1
Embed Size (px)
Citation preview
Integer Types
short month; // half of a machine wordint car; // one machine wordunsigned long distance;
Character Types char c = ‘\0’; // one byte char EndOfLine = ‘\n’;
Real Types double salary; // two machine wordsfloat wage; // one machine word
Basic Data Types of C
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
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 pipv = 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 pippi2
Pointers – More
Pointer Arithmetic
double *p;
loc 0 loc k
p p+k
sizeof (double)
Data type Current address New address
charintdouble
p = 5000p = 5000
p = 5000
p + 1 =p + 1 =p – 6 =
5000 + 1*2 = 50025001
5000 – 6*4 = 4976
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
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.
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
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; }
Returns the size of an object or type name in bytes.
sizeof (type name); sizeof (object); sizeof object;
#include <cstddef>
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
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.
Traversal of an Array throughPointer Manipulation
#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; }}
// 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 ); }
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 arrayvoid 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 elementsvoid putValues (const int [ 10 ]);
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); // wrongvoid putValues (int **matrix, int rowSize, int colSize); // ok now
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
Dynamic memory allocation: memory is allocated at run-time.
int *pi = new int; // pi →
int *pi = new int (1024); // allocates and initializes an objectint *pia = new int [10]; // allocates an array of ten integers
delete pi; delete [ ] pia;
(int)
new and delete
Reference Type
A reference serves as an alternative name for an object.
int ival = 1024;
int &refVal = ival; // ok: refVal is a reference to ivalint &refVal; // error: a reference must be initialized to an object
refVal += 2; // okint *pi = &refVal; // initialize pi with the address of ival
Similar to the use of a pointer but without resorting to pointer syntax.
References (Cont’d)
// defines two objects of type intint ival = 1024, ival2 = 2048;
// defines one reference and one objectint &rval = ival, rval2 = ival2;
// defines one object, one pointer, one referenceint ival3 = 1024, *pi = &ival3, &ri = ival3;
// defines two referencesint &rval3 = ival3, &rval4 = ival2;
1024
1024
2048
ival ival2rval
2048
rval2 ival3
pi rval3
rval4
ri
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);
Initialization (1)
int a[ ] = { 3, 8, -4, 6 };
int &b = a[0];
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].
b
reference to a variable
Initialization (2)
int *c = &(a[3]);
int *&d = c;
3 8 -4 6
a[0] a[1] a[2] a[3]
bc is a pointer pointingat a[3].
c
d is a reference (different name) for thememory 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
Initialization (3)
int **e = &d; 3 8 -4 6
a[0] a[1] a[2] a[3]
be is a pointer pointingat the memory locationlabeled by d, i.e., by c.
c d
e
address
e is a pointer to a pointer.
Update (1)
b--; 3 8 -4 6
a[0] a[1] a[2] a[3]
b
c d
e
*d += b + a[1];
a[0]--; 2 16
a[3] += a[0] + a[1];
Update (2)
c = &(a[1]); 2 8 -4 16
a[0] a[1] a[2] a[3]
b
c d
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];