COMP 21000 Introduction to Computer Organization Systems Chap







![Arrays (review) // ex 2. c int main() { int line[SIZE]; int i, n; Arrays (review) // ex 2. c int main() { int line[SIZE]; int i, n;](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-8.jpg)



![Arrays and Pointers int main() { int line[SIZE]; we’re still allocating the array statically… Arrays and Pointers int main() { int line[SIZE]; we’re still allocating the array statically…](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-12.jpg)









![Strings as arrays #include <stdio. h> int strequal(char x[ ], char y[ ]) { Strings as arrays #include <stdio. h> int strequal(char x[ ], char y[ ]) {](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-22.jpg)


![Strings: readline #include <stdio. h> #define SIZE 100 int readline(char s[ ], int max) Strings: readline #include <stdio. h> #define SIZE 100 int readline(char s[ ], int max)](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-25.jpg)
![Strings: readline int main() { char line[SIZE]; int n; while ( (n = readline(line, Strings: readline int main() { char line[SIZE]; int n; while ( (n = readline(line,](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-26.jpg)















![2 D Arrays /* cannot return an array! */ void vector_add(int a[][SIZE], int n) 2 D Arrays /* cannot return an array! */ void vector_add(int a[][SIZE], int n)](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-42.jpg)


- Slides: 44

COMP 21000 Introduction to Computer Organization & Systems Chap 2. 2 Topics: n C pointers n Pointers to arrays

Pointers • Why learn pointers? – pointers are necessary for dynamic memory – dynamic memory is necessary for efficiency – even with static memory, pointers are theoretically more efficient • actually, compilers do a great job weeding out inefficiencies • even if you run a compiler without optimization it will generate code for array syntax that is the same as code for pointer syntax – helps us to understand how the hardware (memory) works 2 -2

Pointers #include <stdio. h> Pointer declaration int main() { int n 1, n 2, *int. Ptr; Pointer assignment int. Ptr = &n 1; printf ( "Enter two numbersn"); scanf("%d%d", &n 1, &n 2); printf ( "The numbers you entered are: %d and %d n", n 1, n 2); printf ("n 1 is %dn", *int. Ptr); int. Ptr = &n 2; Accessing value that pointer points at printf ( "n 2 is %dn", *int. Ptr); *int. Ptr = n 1 + n 2; printf ( "and their sum is: %dn”, *int. Ptr); } Changing a value in variable that pointer points at See Student/examples/pointers/ptr 1. c 2 -3

dynamic memory • Two types of memory: static and dynamic • Static – allocated to your program when it is loaded – never changes • dynamic – requested by your program when needed – comes and goes as your program runs 2 -4

dynamic memory • Consider MS Word – a very large program – must store the words in a document in memory – how much memory do you allocate? How do you know? 2 -5

Dynamic Memory Example #include <stdio. h> #include <stdlib. h>›//need this library int main() { int *int. Ptr; // int. Ptr is a pointer to an int int. Size = sizeof(int); int. Ptr = (int *)malloc(int. Size); // must first allocate memory printf("Enter an integer: "); scanf("%d", int. Ptr); printf("You entered %dn", *int. Ptr); // why no & in front of int. Ptr? return 0; } See Student/comp 210/examples/pointers/ptr 2. c 2 -6

Pointers & dynamic memory sizeof is a library function that returns the number of bytes in the data type given as an arg #include <stdio. h> int main() (int *)malloc(num. Of. Bytes); { int n 1, n 2, *int. Ptr; int num. Of. Bytes = sizeof(int); malloc returns (void *) by default; int. Ptr = (int *)malloc(num. Of. Bytes); we must cast to an (int *) printf ( "Enter two numbersn"); argument is the number scanf("%d%d", &n 1, &n 2); of bytes to allocate. printf ( "The numbers you entered are: %d and %d n", n 1, n 2); *int. Ptr = n 1 + n 2; printf ( "and their sum is: %dn”, *int. Ptr); free(int. Ptr); return 0; } Must always free dynamically allocated memory! See Student/comp 210/examples/pointers/ptr 3. c 2 -7
![Arrays review ex 2 c int main int lineSIZE int i n Arrays (review) // ex 2. c int main() { int line[SIZE]; int i, n;](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-8.jpg)
Arrays (review) // ex 2. c int main() { int line[SIZE]; int i, n; You do have to specify the size of an array that is a variable (unless you want to use dynamic memory) n = readints(line, SIZE); printf("The numbers you entered are: n"); for (i = 0; i < n; i++) { printf("%dn", line[i]); } } 2 -8

Arrays (review) // ex 2. c #include <stdio. h> #define SIZE 5 You don’t have to specify the size of an array that is a parameter int readints(int s[ ], int max) { int c, i=0; printf("Enter %d numbers: n", max); while (i < max) scanf("%d", &s[i++]); return(i); } 2 -9

Pointer Arithmetic • Incrementing a pointer causes it to be changed by the appropriate number of memory locations int *int. Ptr; int arr[10]; int. Ptr = arr; • int. Ptr points at arr[0] int. Ptr++; // or int. Ptr = int. Ptr + 1; • int. Ptr now points at arr[1] 2 -10

Pointer Arithmetic #include <stdio. h> #define SIZE 5 int main() { int *int. Ptr; int i, arr[SIZE]; int. Ptr = arr; for(i = 0; i < SIZE; i++) { arr[i] = i * 11; } printf ( "arr[0] = %dn", *int. Ptr); printf ( "int. Ptr = %pn", int. Ptr); int. Ptr++; printf ( "arr[1] = %dn", *int. Ptr); printf ( "int. Ptr = %pn", int. Ptr); return 0; } %p means print a pointer 2 -11 * This example is on the server at /home/barr/Student/comp 210/examples/pointer_arith. c
![Arrays and Pointers int main int lineSIZE were still allocating the array statically Arrays and Pointers int main() { int line[SIZE]; we’re still allocating the array statically…](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-12.jpg)
Arrays and Pointers int main() { int line[SIZE]; we’re still allocating the array statically… int i, n, *int. Ptr; int. Ptr = line; remember that array names are pointers! n = readints(line, SIZE); printf("The numbers you entered are: n"); for (i = 0; i < n; i++) { printf("%dn", (*int. Ptr)++ ); } …but we’re using pointer arithmetic to access an array } * This example is on the server at /home/barr/Student/comp 210/examples/pointers/array_ptr 1. c Compile this into a. out with the command: gcc array_ptr 1. c readints. c 2 -12

Arrays and Pointers for (i = 0; i < n; i++) assume that n is 5 { printf("%dn", *(int. Ptr++)); } remember pointer arithmetic! int. Ptrint. Ptr index 0 1 2 3 4 value 111 222 333 444 555 2 -13

Arrays and Pointers #include <stdio. h> #define SIZE 5 An array is a pointer and vice versa int readints(int *s, int max) { int c, i=0; printf("Enter %d numbers: n", max); while (i < max){ scanf("%d", s++); i++; notice that there’s no ‘&’ before s. Why? } return(i); } 2 -14

Arrays and Pointers notice that there’s no ‘&’ before s. Why? while (i < max){ scanf("%d", s++); i++; } s s 1 s s s 2 3 4 index 0 value 111 222 333 444 555 2 -15

Arrays and Pointers • Note that this is valid syntax: #include <stdio. h> int main() { int arr[10]; } Try it! 5[arr] = 11; printf("arr[5] = %dn", arr[5]); 2 -16

Passing Arrays & dynamic memory #include <stdlib. h> #include <stdio. h> malloc returns a (void *), so it must be cast. int main() { int *line; must determine int i, n, *int. Ptr, the. Size; how much storage printf("Enter the size of the arrayn"); we need scanf("%d", &the. Size); line = (int *) malloc(the. Size * sizeof(int)); int. Ptr = line; n = readints(line, the. Size); printf( "The numbers you entered are: n"); Using pointer arithmetic to access an array for (i = 0; i < n; i++) { printf("%dn", *int. Ptr++); } free(line); // always dynamically allocated storage Note thatfree memory that is allocated must be freed! } * This example is on the server at /home/barr/Student/comp 210/examples/array_pass_ptr. c Compile this into a. out with the command: gcc array_pass. c readints. c 2 -17

Arrays and Pointers dynamic memory #include <stdio. h> An array is a pointer and vice versa int readints(int *s, int max) { int c, i=0; printf("Enter %d numbers: n", max); while (i < max) scanf("%d", &s[i++]); return(i); } 2 -18

Returning Arrays* // array_return. c #include <stdio. h> // needed for malloc #include <stdlib. h> #define SIZE 5 Compile this into a. out with the command: gcc array_return. c readints 2. c must have a function prototype if you’re going to define the function after main( ) int *readints 2(int max); int main() { int i; int *line; readints will return a pointer to a new array To receive an array, just use the pointer variable name line = readints(SIZE); printf("The numbers you entered are: n"); for (i = 0; i < SIZE; i++) { printf("%dn", *(line + i)); } } notice the pointer arithmetic. * This example is on the server at /home/barr/Student/comp 210/examples/array_return. c 2 -19

Returning Arrays Must return a pointer to an int* readints 2(int max) { int c, i=0; int *new. Array; Must use a pointer variable Must allocate memory new. Array = (int *)malloc(sizeof(int)*SIZE); printf("Enter %d numbers: n", max); while (i < max) { scanf("%d", &new. Array[i++]); Return the pointer } return(new. Array); } * This example is on the server at /home/barr/Student/comp 210/examples/readints 2. c 2 -20

Strings #include <stdio. h> int lower(int c) { if (c >= 'A' && c <= 'Z') return(c + 'a' - 'A'); else return(c); } int main() { int c; printf("Enter some characters. To end input hit ^D on a new linen"); /* read char until end of file */ while ( (c = getchar()) != EOF) putchar(lower(c)); } 2 -21
![Strings as arrays include stdio h int strequalchar x char y Strings as arrays #include <stdio. h> int strequal(char x[ ], char y[ ]) {](https://slidetodoc.com/presentation_image_h2/7f69ffd4e3b500cba3357a400c72522c/image-22.jpg)
Strings as arrays #include <stdio. h> int strequal(char x[ ], char y[ ]) { int i=0; if (x == y) return(1); while (x[i] == y [i]) { if (x[i] == '