Presentation is loading. Please wait.

Presentation is loading. Please wait.

Initializing variables

Similar presentations


Presentation on theme: "Initializing variables"— Presentation transcript:

1 Initializing variables
Initialization: Giving something a value for the first time General rule: variables MUST be initialized before their values are used (Do not assume that the compiler initializes them to 0) Various ways to initialize when declaring a variable int i = 1; assignment statement i = 2*j*j; scanf (or any other function call using &) scanf("%i",&i); as a formal parameter in a function formal parameters are initialized with the values of the actual parameters. 142 N -1

2 Initialization Quiz initialization Consider: of a (with actual value)
void init_example(int a) { int b, c, d=10, e[5]; b = 5; d = 6; scanf("%d%d",&b,&c); } initialization of d initialization of b initialization of c Where is each of a, b, c, d and e initialized? Note that e is has not been initialized. How to initialize an array? 142 N -2

3 Array Initializers When declaring: int w[4] = {1, 2, 30, -4};
w has size 4 all 4 elements are initialized char vowel[6]={'a','e','i','o','u'}; vowel has size 6 Only 5 elements are initialized vowel[5] is not initialized Beware: Can only use the above notation in a declaration. Illegal in an assignment statement w = {1,2,30,-4}; /* illegal */ Can also write a declaration as: double x[] = {1.0, 3.0, -15.0}; x has size 3 All 3 elements are initialized However can't write (except see later...): double x[]; /* illegal */ 142 N -3

4 Array elements as parameters
When using an array element as a function parameter, treat the array element as a regular variable: e.g. printf("grades are %f and %f", grade[0],grade[1]); same syntax as for a regular variable swap(&grade[i], &grade[i+1]); 142 N -4

5 Arrays as parameters Can also pass a whole array as a parameter
#define ARRAY_SIZE 200 ... double average(int a[ARRAY_SIZE]) { int i, total=0; for(i=0; i<ARRAY_SIZE; i++) total = total + a[i]; return ((double)total/(double)ARRAY_SIZE); } /* in another function */ int x[ARRAY_SIZE]; x_avg = average(x); optional just write the name of the array 142 N -5

6 Arrays as output parameters
a program to sum vectors void vector_sum(int a[3], int b[3], int vsum[3]) { int i; for(i=0; i<3; i++) vsum[i] = a[i] + b[i]; } int main(void) int x[3]={1,2,3}, y[3]={4,5,6}; int z[3]; vector_sum(x,y,z); printf("%d %d %d",z[0],z[1],z[2]); return 0; no & Why? The name of an array is a pointer to the first element of the array. 142 N -6

7 Array parameter summary
Passing array elements: Just like regular variables passed by value can pass the address if one uses & Passing whole arrays: Whole arrays are NOT passed by value (not copied) as a formal parameter type array_name[SIZE] or type array_name[] as an actual parameter array_name no & or [] 142 N -7

8 An Array as a pointer In the memory: for an array A of size n A[0]
... pointed to by A (the value of A is the address of the box that holds A[0]) What is *(A+i) ? A[i] 142 N - 8

9 Pointer Arithmetic add an integer to a pointer Can
subtract an integer from a pointer subtract a pointer from a pointer *((iptr1 + iptr2)/2) /* Error */ *(iptr1 + (iptr2-iptr1)/2) /* OK */ an integer 142 N -9

10 Pointer arithmetic int *iptr;
If iptr is 1031 and integers are stored on 2 bytes what is iptr + 1 ? one byte ( = 8 bits) 1031 1032 1033 1034 1035 another integer iptr+1 one integer iptr pointer arithmetic is done in units of the pointer's base type. This is why *(A+i) is the same as A[i] 142 N -10


Download ppt "Initializing variables"

Similar presentations


Ads by Google