Computer Organization and Design Pointers, Arrays and Strings in C

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Arrays and Pointers in C Alan L. Cox
Pointers CSE 2451 Rong Shi.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Computer Organization and Design Addressing Modes Montek Singh Wed, Sep 19, 2012 Lecture 6.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic Allocation in C
User-Written Functions
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Quiz 11/15/16 – C functions, arrays and strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
C Basics.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Pointers and References
Object Oriented Programming COP3330 / CGS5409
Chapter 16 Pointers and Arrays
C Arrays.
Computer Organization and Design Addressing Modes
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Pointers and Arrays.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Memory Allocation CS 217.
Operands and Addressing Modes
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
Chapter 16 Pointers and Arrays
Homework Starting K&R Chapter 5 Good tutorial on pointers
C++ Pointers and Strings
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Java Programming Language
Exercise Arrays.
Computer Organization and Design Addressing Modes
C++ Pointers and Strings
Operands and Addressing Modes
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
August 31 addresses Drop box Questions? 31 August 2004
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 22, 2017 Lab 4 supplement

C vs. Java For our purposes C is almost identical to Java except: C has “functions”, JAVA has “methods” function == method without “class” i.e., a global method C has “pointers” explicitly Java has them (called “references”) but hides them under the covers JVM takes care of handling pointers, so the programmer doesn’t have to C++ is sort of in-between C and Java In this class, we will see how pointers/references are implemented in C

What is a “pointer” in C? A pointer is an explicit memory address Example int i i is an integer variable in memory located at, say, address 1056 int *p p is a variable that “points to” an integer p is located at, say, address 2004 p = &i the value in p is now equal to the address of variable i i.e., the value stored in Mem[2004] is 1056 (the location of i) Memory address i 1056 1060 1064 1068 2000 p 1056 2004

Referencing and Dereferencing Referencing an object means … taking its address and assigning it to a pointer variable (e.g., p = &i) Dereferencing a pointer means … going to the memory address pointed to by the pointer, and accessing the value there (e.g., *p) Example int i; // i is an int variable int *p; // p is a pointer to int p = &i; // referencing i // p is assigned 1056 *p = 5; // dereference p // i assigned 5 Memory address i 5 1056 1060 1064 1068 2000 p 1056 2004

Pointer expressions and arrays Dereferencing could be done to an expression So, not just *p, but can also write *(p+400) accesses memory location that is 400th int after i Arrays in C are really pointers underneath! int a[10]; // array of integers a itself simply refers to the address of the start of the array a is the same as &a[0] // address of a[0] a is a constant of type “int *” a[0] is the same as *a a[1] is the same as *(a+1) a[k] is the same as *(a+k) a[j] = a[k]; is the same as *(a+j) = *(a+k);

Pointer arithmetic and object size IMPORTANT: Pointer expressions automatically account for the size of object pointed to Example 1 if p is of type “int *” and an int is 4 bytes long if p points to address 1056, (p+2) will point to address 1064 C compiler automatically does the multiply-by-4 BUT… in machine language, we will have to explicitly do the multiply-by-4 Example 2 char *q; // char is 1 byte q++; // really does add 1 Memory address i 1056 1060 1064 1068 2000 p 1056 1064 2004

Pointer examples int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer p = &i; // & means address of p = a; // a means &a[0] p = &a[5]; // address of 6th element of a *p // value at location pointed by p *p = 1; // change value at that location *(p+1) = 1; // change value at next location p[1] = 1; // exactly the same as above p++; // step pointer to the next element

Pointer pitfalls int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer(s) So what happens when p = &i; What is value of p[0]? What is value of p[1]? Very easy to exceed bounds C has no bounds checking!

Iterating through an array 2 ways to iterate through an array using array indices void clear1(int array[], int size) { for(int i=0; i<size; i++) array[i] = 0; } using pointers void clear2(int *array, int size) { for(int *p = &array[0]; p < &array[size]; p++) *p = 0; or, also using pointers, but more concise (more cryptic!) void clear3(int *array, int size) { int *arrayend = array + size; while(array < arrayend) *array++ = 0;

Pointer summary In the “C” world and in the “machine” world: Examples: a pointer is just the address of an object in memory size of pointer itself is fixed regardless of size of object to get to the next object: in machine code: increment pointer by the object’s size in bytes in C: increment pointer by 1 to get the ith object: in machine code: add i*sizeof(object) to pointer in C: add i to pointer Examples: int R[5]; // 20 bytes storage R[i] is same as *(R+i) int *p = &R[3] is same as p = (R+3) (p points 12 bytes after start of R)

Strings in C There is no string type in C 😞 What?! Only low-level support for strings as character arrays char s[] // array of characters char *s // pointer to the beginning of a string But a rich library of string processing functions (string.h) reading: scanf(), fgets(), etc. printing: printf(), puts(), etc. processing: strcpy(), strlen(), strcat(), strcmp()

Strings in C: NULL termination All C functions assume string terminates with a NULL NULL = ASCII 0 character also written as ‘\0’ Example: “hello” is actually { ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ } uses 6 characters, not 5 so, for a string with N real characters, declare it as char S[N+1] but C functions define its length as 5 strlen(“hello”) returns 5 fputs(), printf(), etc. will print the string until they see a ‘\0’ Be mindful of the terminating character!

Declaring strings statically Statically (if size is known at compile time) char S[6] = “hello”; char S[] = “hello”; // compiler puts in the size char string_array[5][10]; an array of 5 strings, each up to 9 characters long (plus NULL) string_array[0] is the 0-th (very first) string string_array[4] is the 4th (last) string string_array[0][4] is the 4th character of the first string etc.

Declaring strings dynamically Dynamically (if size is known only at run time) declare a pointer char *s; determine size (at run time), e.g., 99 real characters + NULL size is 100 allocate space in memory s = malloc(100); BUT remember: there is no bounds checking, so do not put a string with more than 99 real characters in s… Comparison to Java char *s = malloc(100); // in C char[] s = new char[100]; // in Java When finished with string free(s); // free up the 100 bytes Java: no need to free; JVM does garbage collection

Declaring strings statically/dynamically Examples Declare an array of NUM strings, each at most LEN long (incl. the terminating NULL), where NUM and LEN are known at compile time char string_array[NUM][LEN]; Declare array of NUM strings, where only NUM is known at compile time char *string_array[NUM]; string_array[0] = malloc(length of string 0…); string_array[1] = malloc(length of string 1…); etc. Declare an array of strings, where we don’t know how many strings there will be, and how long each will be, at compile time char **string_array; string_array = malloc(how many strings … * sizeof(char *));

Declaring strings statically Declare an array of NUM strings, each at most LEN long (incl. the terminating NULL), where NUM and LEN are known at compile time char string_array[NUM][LEN]; char string_array[3][10]; H e l l o \n \0 # $ ( H i \n \0 # $ ( ! ^ * A l o h a ! ! ! \n \0

Declaring strings dynamically Declare array of NUM strings, where only NUM is known at compile time char *string_array[NUM]; string_array[0] = malloc(length of string 0…); string_array[1] = malloc(length of string 1…); etc. char *string_array[3]; H i \n \0 H e l l o \n \0 A l o h a ! ! ! \n \0

Declaring strings dynamically For sorting: Swap strings by swapping pointers, not by copying contents! char *temp; // pointer to char temp = string_array[i]; // swap pointers! string_array[i] = string_array[j]; // no copying chars string_array[j] = temp;

Passing pointers as arguments to functions One more thing… Passing pointers as arguments to functions

Passing arguments by reference Example 1: reading values into variables int x, y, z; scanf(“%d%d%d”, &x, &y, &z); scanf() changes the values of x, y and z! How? we provide it the addresses where x, y and z are located Example 2: a function to double the value given to it void double_it(int x) { x = x*2; } doesn’t work… because x is “passed by value” a copy of x is modified inside the function, not the original

Passing arguments by reference Example 2 take 2: a function to double the value given to it void double_it(int *x) { *x = (*x) * 2; } int main() { int y = 10; double_it(&y); // y is now 20 works! Because y is “passed by reference” the address in memory where y is stored is sent to the function, which modifies it correctly at that location

Passing arguments: C vs. Java Which of these will work in Java? WHY? Modify an integer double_it(int x) { x = x*2; } Modify an object double_it(Point p) { p.x = p.x*2; } Swap two objects swap(Point p1, Point p2) { Point temp = p1; p1 = p2; p2 = temp; }

Passing arguments: C vs. Java passes arguments by value for all the primitive data types int, double, float, etc. double_it(int x) { x = x*2; } // won’t work passes objects by reference so member data of an object can be modified double_it(Point p) { p.x = p.x*2; } // works! BUT: references are passed by value to methods… so cannot swap two objects swap(Point p1, Point p2) { Point temp = p1; p1 = p2; p2 = temp; // won’t work! } Understanding C pointers will demystify everything!