OneDimensional Arrays Often programs use homogeneous data As

  • Slides: 6
Download presentation
One-Dimensional Arrays • Often, programs use homogeneous data. As an example, if we want

One-Dimensional Arrays • Often, programs use homogeneous data. As an example, if we want to manipulate some grades, we might declare • int grade 0, grade 1, grade 2; • If we have a large number of grades, it becomes cumbersome to represent/manipulate the data using unique identifiers. • Arrays allow us to refer to a large number of the same data type using a single name. For instance, • int grade[3]; • Makes available the use of int variables grade[ 0], grade[1], grade[2] in a program. Note that arrays are zero-indexed (numbering always starts at 0). • Now, to access elements of this array, we can write grade[expr], where expr is an integral expression. • Example: (fragment) • for( i = 0; i < 3; i++ ) • sum += grade[i]

Pointers • A variable in a program is stored in a certain number of

Pointers • A variable in a program is stored in a certain number of bytes at a particular memory location, or address, in the machine. Pointers allow us to manipulate these addresses explicitly. Two unary operators: (“inverses”) • • – & operator – “address of”. Can be applied to any variable. “Adds a star to type”. – * operator – “information at”. Can be applied only to pointers. “Removes a star from type” int a = 1, b = 2, *p; void *void_p; char *char_p; p = &a; b = *p; • • An assignment like char_p = &a; is illegal, as the types do not match. void is a generic pointer type; can make assignments such as void_p = char_p; or void_p = &b;

Constructs not to be pointed at • Do not point at constants: – –

Constructs not to be pointed at • Do not point at constants: – – int *ptr; ptr = &3; /* illegal */ • Do not point at arrays; an array name is a constant. – – – int a[77]; void *ptr; ptr = &a; /* illegal */ • Do not point at expressions that are not variables. – – int k = 1, *ptr; ptr = &(k + 99); /* illegal */

 • • • “Call by reference” (not really) Pointers allow us to perform

• • • “Call by reference” (not really) Pointers allow us to perform something similar to call-by-reference (technically, we are passing pointers/references by value) “call-by-reference” allows a function to make changes to a variable that persist Examples. void set_int_to_3( int *p ) { *p = 3; } /* Q: How to call this function? void swap( int *p, int *q ) { int temp; temp = *p; *p = *q; *q = temp; } */

Arrays and Pointers • Assume int i, a[10], *p; – The type of a

Arrays and Pointers • Assume int i, a[10], *p; – The type of a is “int *”. – a is equivalent to – a + i is equivalent to &a[0] &a[i] • Correspondingly, – a[i] is equivalent to *(a + i) p[i] is equivalent to *(p + i) • In fact, – for( p = a; p < &a[10]; p++ ) sum += *p; for( i = 0; i < 10; i++ ) sum += *(a + i); p = a; for( i = 0; i < 10; i++ ) sum += p[i];

Example: Arrays as Function Arguments (Array passed by reference, so changes to array persist)

Example: Arrays as Function Arguments (Array passed by reference, so changes to array persist) void main() { int a[5] = {0, 1, 2, 3, 4}; printf( “sum of a: %dn”, change_and_sum( a, 5 )); printf( “value of a[0]: %dn”, a[0] ); } int change_and_sum( int a[], int size ) { int i, sum = 0; a[0] = 100; for( i = 0; i < size; i++ ) sum += a[i]; return sum; }