Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise Arrays.

Similar presentations


Presentation on theme: "Exercise Arrays."— Presentation transcript:

1 Exercise Arrays

2 Arrays A block of many variables of the same type
Array can be declared for any type E.g. int A[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices

3 Arrays in Memory Sequence of variables of specified type
The array variable itself holds the address in memory of beginning of sequence Example: double S[10]; The k-th element of array A is specified by A[k-1] (0-based) 1 2 3 4 5 6 7 8 9 S

4 Initialization Like in the case of regular variables, we can initialize the array during declaration. the number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0

5 Initialization if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations : int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};

6 Example Sort.c

7 Sort – step by step #include <stdio.h> #define SIZE 3
A[0] A[1] A[2] Sort – step by step 4 8 2 min_index ---- i j tmp #include <stdio.h> #define SIZE 3 int main(void) { int A[SIZE] = {4,8,2}; int min_index; int i,j,tmp; ---- ---- ----

8 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index ---- i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } ---- ----

9 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index i j tmp
i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } ---- ----

10 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index i j tmp
i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 ----

11 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index i j tmp
i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 ----

12 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index i j tmp
i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 2 ----

13 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 2 ----

14 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 3 ----

15 Sort – step by step A[0] A[1] A[2] 4 8 2 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 3 4

16 Sort – step by step A[0] A[1] A[2] 2 8 2 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 3 4

17 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 3 4

18 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

19 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 1 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

20 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 1 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

21 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

22 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

23 Sort – step by step A[0] A[1] A[2] 2 8 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

24 Sort – step by step A[0] A[1] A[2] 2 4 4 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

25 Sort – step by step A[0] A[1] A[2] 2 4 8 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

26 Sort – step by step A[0] A[1] A[2] 2 4 8 min_index 2 i j tmp
for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 2 3 8

27 Exercise Write a program that gets an input line from the user (ends with ‘\n’) and displays the number of times each letter appears in it. Example: For the input line: hello, world! The output should be: d – 1 e – 1 h – 1 l – 3 o – 2 w – 1 r – 1 Assume that the input is all in lower-case.

28 Solution letter_count.c

29 Arrays as function arguments
Functions can accept arrays as arguments Usually the array’s size also needs to be passed (why?)

30 Arrays as function arguments
For example: int CalcSum(int arr[], int size); Within the function, arr is accessed in the usual way Changes in the function affect the original array!!

31 Example calc_sum.c

32 Exercise Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise Write a program that accepts two arrays of integers from the user and checks for equality

33 Solution compare_arrays.c

34 Strings An array of characters Used to store text
Another way to initialize: char A[ ]=“blabla”;

35 The Terminator char A[ ] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};
Strings terminate with NULL character, signed by ‘\0’ (ascii code 0) This is a convention used to know where the string ends It means that in order to hold a string of 7 chars we need an array of length 8 So the previous initialization is equivalent to char A[ ] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};

36 The fool on the null #include <stdio.h> int main(void) {
char str[]="I'm a full string"; printf("%s\n", str); str[7]='o'; str[8]='o'; str[11]='\0'; str[11] = ‘s’; return 0; }

37 Reading-in strings: scanf
To read in a string to a variable str, write : scanf(“%s”, str); Note there’s no ‘&’ sign!!!

38 Reading-in strings - scanf
scanf reads-in letters until a space or newline (‘\n’) is encountered The maximum length can be stated in the parentheses: scanf(“%10s”, str); This will read in 10 letters, plus the ‘\0’ sign (so str should have place for 11 characters)

39 Example Compare.c

40 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

41 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

42 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

43 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

44 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

45 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

46 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

47 Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

48 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

49 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

50 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

51 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

52 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

53 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

54 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

55 Equal strings – step by step
‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

56 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i

57 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i

58 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

59 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

60 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 2

61 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

62 Different length – step by step
A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

63 String library Like in the case of stdio.h and math.h, we have a special library for handling strings We should #include string.h Functions: strlen(s) – returns the length of s strcmp(s1, s2) – compares s1 with s2 strcpy(s1, s2) – copies to contents of s2 to s1 and more…

64 Example easy_compare.c

65 If there’s time… Exercise: write a function:
void my_to_lower(char str[]); The function receives an arbitrary string and converts all its letters to lower-case letters Demonstrate the use of your function by some example that prints the input and output on the screen

66 Pointers Pointer is a variable that contains the address of a variable
Here P is said to point to the variable C C 7 3 4 173 172 174 175 176 177 178 179 180 181 P 833 832 834 835 836 837 838 839 840 841

67 Referencing The unary operator & gives the address of a variable
The statement P = &C; assigns the address of C to the variable P, and now P points to C To print a pointer, use %p format.

68 Referencing int C; int *P; /* Declare P as a pointer to int */ C = 7;
P = &C; C 7 3 4 172 173 174 175 176 177 178 179 180 181 P 174 3 4 832 833 834 835 836 837 838 839 840 841

69 Dereferencing The unary operator * is the dereferencing operator
Applied on pointers Access the object the pointer points to The statement *P = 5; puts in C (the variable pointed by P) the value 5

70 Dereferencing printf(“%d”, *P); /* Prints out ‘7’ */ *P = 177;
printf(“%d”, C); /* Prints out ‘177’ */ P = 177; /* This is unadvisable!! */ C 177 7 3 4 172 173 174 175 176 177 178 179 180 181 P 174 177 3 4 832 833 834 835 836 837 838 839 840 841

71 Example pointers.c

72 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 564 772

73 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

74 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

75 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

76 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

77 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

78 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772

79 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 372 564 772

80 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 372 564 772

81 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772

82 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772

83 pointers.c – step by step
x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772

84 Common errors It is impossible to define pointers to constants or expressions. It is also impossible to change a variable’s address (because it is not for us to determine!). Therefore, the following are errors: i = &3; j = &(k+5); k = &(a==b); &a = &b; &a = 150;

85 Pointers and Arrays Recall that an array S holds the address of its first element S[0] S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0]

86 Pointer-array equivalence
Arrays are actually a kind of pointers! When an array is defined, a fixed amount of memory (the size of the array) is allocated. The array variable is set to point to the beginning of that memory segment When a pointer is declared, it is uninitialized (like a regular variable) Unlike pointers, the value of an array variable cannot be changed

87 Pointer arithmetic Pointers can be incremented and decremented
If p is a pointer to a particular type, p+1 yields the correct address of the next variable of the same type p++, p+i, and p += i also make sense

88 Pointer arithmetic - example
If p and q point to elements in an array, q-p yields the number of elements between p and q. However, there is a difference between pointer arithmetic and “regular” arithmetic. Example – ptr_arithmetic.c


Download ppt "Exercise Arrays."

Similar presentations


Ads by Google