Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP 21000 Spring 2014 C Part IV.

Similar presentations


Presentation on theme: "Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP 21000 Spring 2014 C Part IV."— Presentation transcript:

1 Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP 21000 Spring 2014 C Part IV

2 Pointers Why learn pointers? – pointers are necessary for dynamic memory – dynamic memory is necessary for efficiency – even with static memory, pointers are more efficient – real efficiency comes at the hardware/software interface (don’t listen to Ali!) – helps us to understand how the hardware (memory) works 2-2

3 Pointers #include int main() { int n1, n2, *intPtr; intPtr = &n1; printf ( "Enter two numbers\n"); scanf("%d%d",&n1,&n2); printf ( "The numbers you entered are: %d and %d \n", n1, n2); printf ("n1 is %d\n", *intPtr); intPtr = &n2; printf ( "n2 is %d\n",*intPtr); *intPtr = n1 + n2; printf ( "and their sum is: %d\n”, *intPtr); } 2-3 Pointer assignment Accessing value that pointer points at Changing a value in variable that pointer points at Pointer declaration See Student/Examples/pointers/pointer1.c

4 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

5 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

6 Pointers & dynamic memory #include int main() { int n1, n2, *intPtr; int numOfBytes = sizeof(int); intPtr = (int *)malloc(numOfBytes); printf ( "Enter two numbers\n"); scanf("%d%d",&n1,&n2); printf ( "The numbers you entered are: %d and %d \n", n1, n2); *intPtr = n1 + n2; printf ( "and their sum is: %d\n”, *intPtr); free(intPtr); } 2-6 malloc returns (void *) by default; we must cast to an (int *) argument is the number of bytes to allocate. Must always free dynamically allocated memory! (int *)malloc(numOfBytes); sizeof is a library function that returns the number of bytes in the data type given as an arg See Student/Examples/pointers/pointer2.c

7 Arrays (review) // ex2.c int main() { int line[SIZE]; int i, n; n = readints(line, SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < n; i++) { printf("%d\n", line[i]); } 2-7 You do have to specify the size of an array that is a variable (unless you want to use dynamic memory)

8 Arrays (review) // ex2.c #include #define SIZE 5 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-8 You don’t have to specify the size of an array that is a parameter

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

10 Pointer Arithmetic #define SIZE 5 int main() { int *intPtr; int i, arr[SIZE]; intPtr = arr; for(i = 0; i < SIZE; i++) { arr[i] = i * 11; } printf ( "arr[0] = %d\n", *intPtr); printf ( "intPtr = %p\n", intPtr); intPtr++; printf ( "arr[1] = %d\n", *intPtr); printf ( "intPtr = %p\n", intPtr); } 2-10 %p means print a pointer * This example is on the server at /home/barr/Student/Examples/pointers/pointer_arith.c

11 Arrays and Pointers int main() { int line[SIZE]; int i, n, *intPtr; intPtr = line; n = readints(line, SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < n; i++) { printf("%d\n", *(intPtr++)); } 2-11 …but we’re using pointer arithmetic to access an array we’re still allocating the array statically… * This example is on the server at /home/barr/Student/Examples/pointers/array_ptr1.c remember that array names are pointers!

12 Arrays and Pointers for (i = 0; i < n; i++) { printf("%d\n", *(intPtr++)); } 2-12 assume that n is 5 * This example is on the server at /home/barr/Student/Examples/pointers/array_ptr1.c index01234 value111222333444555 intPtr remember pointer arithmetic!

13 Arrays and Pointers #include #define SIZE 5 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-13 An array is a pointer and vice versa notice that there’s no ‘&’ before s. Why?

14 Arrays and Pointers while (i < max){ scanf("%d",s++); i++; } 2-14 notice that there’s no ‘&’ before s. Why? index01234 value sssss 111222333444555

15 Passing Arrays & dynamic memory int main() { int *line; int i, n, *intPtr, theSize; printf("Enter the size of the array\n"); scanf("%d", &theSize); line = (int *) malloc(theSize * sizeof(int)); intPtr = line; n = readints(line, theSize); printf( "The numbers you entered are: \n"); for (i = 0; i < n; i++) { printf("%d\n", *intPtr++); } free(line); // always free dynamically allocated storage } 2-15 must determine how much storage we need Using pointer arithmetic to access an array Note that memory that is allocated must be freed! malloc returns a (void *), so it must be cast. * This example is on the server at /home/barr/Student/Examples/array_pass_ptr.c

16 Arrays and Pointers dynamic memory #include 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-16 An array is a pointer and vice versa

17 Returning Arrays* // array_return.c #include // needed for malloc #include #define SIZE 5 int *readints(int max); int main() { int i; int *line; line = readints(SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < SIZE; i++) { printf("%d\n", *(line + i)); } 2-17 must have a function prototype if you’re going to define the function after main( ) * This example is on the server at /home/barr/Student/Examples/array_return.c readints will return a pointer to a new array To receive an array, just use the pointer variable name notice the pointer arithmetic.

18 Returning Arrays int* readints(int max) { int c,i=0; int *newArray; newArray = (int *)malloc(sizeof(int)*SIZE); printf("Enter %d numbers: \n",max); while (i < max) { scanf("%d",&newArray[i++]); } return(newArray); } 2-18 Must return a pointer to an int Must allocate memory Return the pointer Must use a pointer variable

19 initializing memory loops are slow array access is slow better to deal with memory directly: memset 2-19

20 buffer manipulation 2-20 FunctionDescription memset()It is used to initialize a specified number of bytes to null or any other value in the buffer memcpy()It is used to copy a specified number of bytes from one memory to another memmov()It is used to copy a specified number of bytes from one memory to another or to overlap on same memory. Difference between memmove and memcpy is, overlap can happen on memmove whereas memcpy should be done in non-destructive way memcmp()It is used to compare specified number of characters from two buffers memicmp()It is used to compare specified number of characters from two buffers regardless of the case of the characters memchr()It is used to locate the first occurrence of the character in the specified string

21 buffer set int main() { int i; /* allocate memory for array of 5 elements */ char *a = (char *) malloc(5*sizeof(char)); /* All elements are set to c. It can be set to any value */ memset(a, ‘c’, 5*sizeof(char)); printf("\nValues after memset\n"); for (i = 0; i < 5; ++i) printf(" a[%d] = %d,", i, a[i]); // remove x from memory free(a); return 0; } 2-21 for timing, see example at Student/Examples/chap1/testMemInt.c memset sets bytes. The second arg must be a char (or 0). Cannot use to set integers (other than 0) in an array.

22 2D Arrays #include #define COLS 5 #define ROWS 3 /* function prototype */ void vector_add(int a[][SIZE], int n); main() { int i,j; int arr[ROWS][COLS]; printf("Enter in the first row (5 values): "); for (i = 0; i < ROWS; i++){ for (j = 0; j < COLS; j ++) { scanf("%d", &arr[i][j]); } if (i < numRows -1) printf("\nEnter in the next row: "); } fprintf(stderr,"Calling vector_add\n"); vector_add(arr, 3); printf("\n"); printf("Good bye!\n"); } 2-22 You DO have to specify the size of all dimensions in an array that is a parameter EXCEPT the first.

23 2D Arrays /* cannot return an array! */ void vector_add(int a[][SIZE], int n) { int i,j; int ans[n]; /* initialize ans */ for (i = 0; i < n; i++) ans[i] = 0; fprintf(stderr,"in vector_add\n"); /* sum the rows */ for (i = 0; i < n ; i++) for (j = 0; j < SIZE ; j++) ans[i] = ans[i] + a[i][j]; printf("The sum of each row is: \n"); for (i = 0; i < n; i++) printf("%d ",ans[i]); printf("\n\n"); } 2-23 Cannot return an array. Well, you can return a pointer that points to an array, but we won’t go there. Yet.

24 2D arrays and Pointers #include int main(){ int *line[5], *ptr; int n, i; for (n = 0; n < 5; n++) line[n] = (int *)malloc(4*sizeof(int)); for (n = 0; n < 5; n++){ for (i = 0; i < 4; i++) line[n][i] = n * i; } printf( "The numbers are: \n"); for (n = 0; n < 5; n++){ for (i = 0; i < 4; i++){ printf("%d ", line[n][i]); } printf("\n"); } 2-24 must do a malloc for each element of the line array. 01234 line

25 2D arrays and Pointers #include int main(){ int *line[5], *ptr; int n, i; for (n = 0; n < 5; n++) line[n] = (int *)malloc(4*sizeof(int)); for (n = 0; n < 5; n++){ ptr = line[n]; for (i = 0; i < 4; i++) //line[n][i] = n * i; *ptr++ = n * i; } printf( "The numbers are: \n"); for (n = 0; n < 5; n++){ ptr = line[n]; for (i = 0; i < 4; i++){ printf("%d ”*ptr++,); } printf("\n"); } 2-25 Same as previous slide but using pointer syntax

26 Strings #include 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 line\n"); /* read char until end of file */ while ( (c = getchar()) != EOF) putchar(lower(c)); } 2-26

27 Strings as arrays # include int strequal(char x[ ], char y[ ]) { int i=0; if (x == y) return(1); while (x[i] == y [i]) { if (x[i] == '\0') return (1); i++; } return(0); } 2-27

28 Strings as arrays int main() { char str1[10],str2[10]; printf("Enter a string\n"); scanf("%s",str1); printf("\nEnter another string\n"); scanf("%s",str2); if (strequal(str1,str2)) printf("The strings are equal\n"); else printf("The strings are not equal\n"); } 2-28

29 Strings and Pointers #include int main() { char alpha[ ] = "abcdefghijklmnopqrstuvwxyz"; char *str_ptr; str_ptr = alpha; while(*str_ptr != '\0') printf ("%c ",toupper(*str_ptr++) ); printf("\n"); } 2-29 Strings always end with the ‘\0’ character An array name is a pointer “toupper” is a function defined in the ctype.h library.

30 Strings: readline #include #define SIZE 100 int readline(char s[ ],int max) { int c,i=0; max--; while (i < max && (c = getchar()) != EOF && c != '\n') s[i++] =c; if (c == '\n') s[i++] = c; s[i] = '\0'; return(i); } 2-30

31 Strings: readline int main() { char line[SIZE]; int n; while ( (n = readline(line, SIZE) ) > 0) printf("n = %d \t line = %s\n", n, line); } 2-31

32 Strings and dynamic memory #include /* compare two strings character by character using array syntax */ int strequal(char x[ ],char y[ ]) { int i=0; if (x == y) return(1); while (x[i] == y [i]) { if (x[i] == '\0') return (1); i++; } return(0); } 2-32

33 Strings and dynamic memory int main() { char *str1,*str2; str1 = (char *) malloc(20 * sizeof(char)); str2 = (char *) malloc(20 * sizeof(char)); printf("Enter a string or ! to halt \n"); scanf("%s",str1); while (strcmp(str1, "!") != 0){ printf("\nEnter another string\n"); scanf("%s",str2); if (strequal(str1,str2)) printf("The strings are equal\n"); else printf("The strings are not equal\n"); printf("Enter a string or ! to halt \n"); scanf("%s",str1); } 2-33 Strcmp is a library functionstrequal is a user defined function

34 Strings and pointers int strequal(char *x, char *y) ;// function prototype #define DEBUG 1 int main() { char *str1,*str2; str1 = (char *) malloc(10 * sizeof(char)); str2 = (char *) malloc(10 * sizeof(char)); printf("Enter a string or ! to halt \n"); scanf("%s",str1); #ifdef DEBUG printf("This is a debug statement. str1 is %s \n", str1); #endif while (strcmp(str1, "!") != 0){ printf("\nEnter another string\n"); scanf("%s",str2); if (strequal(str1,str2)) printf("The strings are equal\n"); else printf("The strings are not equal\n"); printf("Enter a string or ! to halt \n"); scanf("%s",str1); } 2-34 If the name DEBUG is #defined, then #ifdef is true and the printf is included. Use #define to put in debug statements

35 Strings and pointers #include int strequal(char *x, char *y) { if (x == y) return(1); while (*x++ == *y++) { if (*x == '\0' && *y == '\0') return (1); } return(0); } 2-35 *x is the contents of what x points at Return true if at end of string


Download ppt "Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP 21000 Spring 2014 C Part IV."

Similar presentations


Ads by Google