Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.

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.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
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 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
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.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Computer Organization and Design Pointers, Arrays and Strings in C
Arrays in C.
Programming Languages and Paradigms
Object Oriented Programming COP3330 / CGS5409
C Arrays.
Computer Organization and Design 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
Exercise Arrays.
Computer Organization and Design Addressing Modes
Operands and Addressing Modes
Pointers, Dynamic Data, and Reference Types
August 31 addresses Drop box Questions? 31 August 2004
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement

C vs. Java  For our purposes C is almost identical to Java except: C has “functions”, JAVA has “methods” C has “functions”, JAVA has “methods”  function == method without “class”  i.e., a global method C has “pointers” explicitly 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 int i  i is an integer variable in memory  located at, say, address 1056 int *p int *p  p is a variable that “points to” an integer  p is located at, say, address 2004 p = &i 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 ) 1056 i p Memory address

Referencing and Dereferencing  Referencing an object means … taking its address and assigning it to a pointer variable (e.g., p = &i ) … 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 ) … 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 i p Memory address

Pointer expressions and arrays  Dereferencing could be done to an expression So, not just *p, but can also write *(p+400) 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 int a[10];// array of integers a itself simply refers to the address of the start of the array 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 the same as &a[0]// address of a[0] a is a constant of type “ int * ” a is a constant of type “ int * ” a[0] is the same as *a a[0] is the same as *a a[1] is the same as *(a+1) a[1] is the same as *(a+1) a[k] is the same as *(a+k) a[k] is the same as *(a+k) a[j] = a[k]; is the same as *(a+j) = *(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 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 Example 2  char *q; // char is 1 byte  q++; // really does add i p Memory address

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 6 th 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! C has no bounds checking!

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

Pointer summary  In the “C” world and in the “machine” world: a pointer is just the address of an object in memory a pointer is just the address of an object in memory size of pointer itself is fixed regardless of size of object size of pointer itself is fixed regardless of size of object to get to the next 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: 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 int R[5]; // 20 bytes storage R[i] is same as *(R+i) 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 ) 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 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) 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 NULL = ASCII 0 character  also written as ‘\0’  Example: “hello” is actually { ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ } “hello” is actually { ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ } uses 6 characters, not 5 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 but C functions define its length as 5  strlen(“hello”) returns 5 fputs(), printf(), etc. will print the string until they see a ‘\0’ 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[6] = “hello”; char S[] = “hello”; // compiler puts in the size char S[] = “hello”; // compiler puts in the size char string_array[5][10]; 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 4 th (last) string  string_array[0][4] is the 4 th character of the first string  etc.

Declaring strings dynamically  Dynamically (if size is known only at run time) 1. declare a pointer  char *s; 2. determine size (at run time), e.g., 99 real characters + NULL  size is 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 = malloc(100); // in C char[] s = new char[100]; // in Java char[] s = new char[100]; // in Java  When finished with string free(s); // free up the 100 bytes free(s); // free up the 100 bytes Java: no need to free; JVM does garbage collection Java: no need to free; JVM does garbage collection

Declaring strings 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 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 an array of NUM strings, where only NUM is known at compile time Declare an 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 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 *) );  string_array[0] = malloc (length of string 0…);  string_array[1] = malloc (length of string 1…);  etc.

An array of strings  Declare an array of NUM strings, where only NUM is known at compile time char *string_array[NUM]; char *string_array[NUM]; string_array[0] = malloc (length of string 0…); string_array[0] = malloc (length of string 0…); string_array[1] = malloc (length of string 1…); string_array[1] = malloc (length of string 1…); etc. etc.  For sorting: Swap strings by swapping pointers, not by copying contents! char *temp; // pointer to char char *temp; // pointer to char temp = string_array[i]; // swap pointers! temp = string_array[i]; // swap pointers! string_array[i] = string_array[j]; // no copying chars string_array[i] = string_array[j]; // no copying chars string_array[j] = temp; string_array[j] = temp;

One more thing… Passing pointers as arguments to functions

Passing arguments by reference  Example 1: reading values into variables 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? 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 a function to double the value given to it void double_it(int x) { x = x*2; x = x*2;} doesn’t work… because x is “passed by value” 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 a function to double the value given to it void double_it(int *x) { *x = (*x) * 2; *x = (*x) * 2;} int main() { int y = 10; int y = 10; double_it(&y); // y is now 20 double_it(&y); // y is now 20} works! Because y is “passed by reference” 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? 1. Modify an integer double_it(int x) { x = x*2; } 2. Modify an object double_it(Point p) { p.x = p.x*2; } 3. Swap two objects swap(Point p1, Point p2) { Point temp = p1; Point temp = p1; p1 = p2; p1 = p2; p2 = temp; p2 = temp;}

Passing arguments: C vs. Java  Java: passes arguments by value for all the primitive data types 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 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… BUT: references are passed by value to methods…  so cannot swap two objects swap(Point p1, Point p2) { Point temp = p1; Point temp = p1; p1 = p2; p1 = p2; p2 = temp; // won’t work! p2 = temp; // won’t work!}  Understanding C pointers will demystify everything!