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

Slides:



Advertisements
Similar presentations
An introduction to pointers in c
Advertisements

Lectures 10 & 11.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Arrays and pointers Lone Leth Thomsen. March 2006Basis-C-5/LL2 What is an array An array is a consecutive series of variables that share one variable.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
CSC Programming for Science Lecture 34: Dynamic Pointers.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2016 C Part IV.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers verses Variables
Stack and Heap Memory Stack resident variables include:
Computer Science 210 Computer Organization
Hassan Khosravi / Geoffrey Tien
Programming Languages and Paradigms
Dynamic Memory Allocation
Computer Science 210 Computer Organization
14th September IIT Kanpur
CSC 270 – Survey of Programming Languages
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CSC215 Lecture Memory Management.
CS111 Computer Programming
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers and Arrays Beyond Chapter 16
Introduction to Computer Organization & Systems
Introduction to Computer Organization & Systems
Introduction to Computer Organization & Systems
Presentation transcript:

Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP 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

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

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

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

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)

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

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

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

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!

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 value intPtr remember pointer arithmetic!

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?

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

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

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

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.

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

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

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

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.

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.

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.

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 line

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

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

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

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

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.

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

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

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

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

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

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