introduction to computer organization & systems topics: c arrays c pointers comp 21000 spring...

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

Upload: margaretmargaret-chandler

Post on 18-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

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

Introduction to Computer Organization & Systems

Introduction to Computer Organization & Systems

Topics:C arraysC pointers

COMP 21000Spring 2014

C Part IV

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

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

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

Pointers#include <stdio.h>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.cSee Student/Examples/pointers/pointer1.c

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

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

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

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

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

Pointers & dynamic memory#include <stdio.h>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);(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.cSee Student/Examples/pointers/pointer2.c

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

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)

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

Arrays (review)// ex2.c

#include <stdio.h>

#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

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

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

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

Pointer Arithmetic#define SIZE 5int 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* This example is on the server at /home/barr/Student/Examples/pointers/pointer_arith.c

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

Arrays and Pointersint 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* This example is on the server at /home/barr/Student/Examples/pointers/array_ptr1.c

remember that array names are pointers!

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

Arrays and Pointersfor (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* This example is on the server at /home/barr/Student/Examples/pointers/array_ptr1.c

index 0 1 2 3 4

value 111 222 333 444 555

intPtrintPtrintPtr intPtr intPtr

remember pointer arithmetic!

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

Arrays and Pointers#include <stdio.h>

#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?

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

Arrays and Pointerswhile (i < max){ scanf("%d",s++);

i++;}

2-14

notice that there’s no ‘&’ before s. Why?

index 0 1 2 3 4

value

s s s s s

111 222 333 444 555

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

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* This example is on the server at /home/barr/Student/Examples/array_pass_ptr.c

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

Arrays and Pointersdynamic memory

#include <stdlib.h>

#include <stdio.h>

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

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

Returning Arrays*// array_return.c

#include <stdio.h>

// needed for malloc

#include <stdlib.h>

#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* 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.

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

Returning Arraysint* 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

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

initializing memory

• loops are slow• array access is slow• better to deal with memory directly: memset

2-19

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

buffer manipulation

2-20

Function Description

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

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

buffer setint 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.

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

2D Arrays#include <stdio.h>#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.

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

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.

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

2D arrays and Pointers#include <stdlib.h>#include <stdio.h>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.

0 1 2 3 4line

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

2D arrays and Pointers#include <stdlib.h>#include <stdio.h>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

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

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 line\n");

/* read char until end of file */ while ( (c = getchar()) != EOF) putchar(lower(c)); }

2-26

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

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] == '\0')

return (1);

i++;

}

return(0);

}

2-27

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

Strings as arraysint 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

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

Strings and Pointers#include <stdio.h>#include <ctype.h>

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.

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

Strings: readline#include <stdio.h>

#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

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

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

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

Strings and dynamic memory#include <string.h>

/* 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

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

Strings and dynamic memoryint 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 function

strequal is a user defined function

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

Strings and pointersint strequal(char *x, char *y) ; // function prototype#define DEBUG 1int 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

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

Strings and pointers#include <stdio.h>

#include <string.h>

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