Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
UNIT 14 Functions with Pointer Parameters.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
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.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
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
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Lecture 5 Pointers 1. Variable, memory location, address, value
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Cinda Heeren / Geoffrey Tien
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Functions and Pointers
Pointers.
Hassan Khosravi / Geoffrey Tien
Dynamic Memory Allocation Strings
CS1010 Programming Methodology
Pointers.
INC 161 , CPE 100 Computer Programming
Functions, variables, operators, loops Modularity
Lecture 6 C++ Programming
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
Pointers and References
14th September IIT Kanpur
C Passing arrays to a Function
5. Arrays, Pointers and Strings
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
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.
Initializing variables
Pointers and Arrays Beyond Chapter 16
C (and C++) Pointers April 4, 2019.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Data Structures and Algorithms Introduction to Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Hassan Khosravi / Geoffrey Tien Pointers September 13, 2017 Hassan Khosravi / Geoffrey Tien

Multi-dimensional arrays A two-dimensional array is specified using two indices First index denotes the row, second index denotes column row col 1 2 3 int marks[3][4]; C stores a two-dimensional array contiguously like a 1D array 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 Multi-dimensional arrays passed as parameters in same way void myfunction(int data[][NUMCOLS], int numrows); September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Function parameters Pass by reference In some cases, parameters may be passed by reference ("call-by-reference") The address (rather than the value) of the actual parameter is copied to the formal parameter when the function is called Making a change to the value of the formal parameter effectively changes the value of the actual parameter This is what occurs with array parameters, which are passed by reference by default More about this when we get to pointers September 13, 2017 Hassan Khosravi / Geoffrey Tien

Addresses and pointers Every storage location in memory (RAM) has an address associated with it The address is the location in memory where a given variable or identifier stores its data Can think of address in memory like a mailbox number Use the address to find where the mailbox is Look inside the mailbox to access the contents/value September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Variable declaration Each byte of memory has a unique address 201 202 203 204 205 206 207 208 209 int a; char c; a = 5; a++; a c At compile time, the compiler knows how much memory to allocate to each variable (e.g. 4 bytes for int, 1 byte for char, etc) September 13, 2017 Hassan Khosravi / Geoffrey Tien

Addresses, &, and pointers You have already encountered addresses with the scanf function scanf requires us to provide the address of a location using the "address of" operator, & e.g. scanf("%d", &a) This allows the scanf function to modify the value of the variable a, which is defined outside of scanf's call stack A pointer is a data type that contains the address of the object in memory, but it is not the object itself a is an integer variable with the value 5 p is a pointer variable storing the address of a int a = 5; int* p = &a; September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Declaring pointers Pointer variables are declared as follows: datatype* identifier e.g. int* ptr; or int * ptr; or int *ptr; Note that the type of a pointer is not the same as the type it points to e.g. ptr is a pointer to an int, but is itself not an int Warning! The declaration int* var1, var2; declares var1 as a pointer, but var2 as an integer! To declare both as pointers, either declare individually, or: int *var1, *var2; September 13, 2017 Hassan Khosravi / Geoffrey Tien

Address operator and dereferencing Pointers can be assigned the address of an existing variable Using the address operator, & The value which a pointer points to can be accessed by dereferencing the pointer Using the * operator p x … 4096 … 47 23 38 1 212 220-1 int x = 23; int* p = &x; x = 47; *p = 38; September 13, 2017 Hassan Khosravi / Geoffrey Tien

Pointers as parameters Function parameters can be passed by reference using pointers int getArraySum(int arr[], int size, int* pcount) { int sum = 0; for (int i = 0; i < size; i++) { if (arr[i] > 0) (*pcount)++; sum += arr[i]; } return sum; int numpositive = 0; int numbers[] = {3, 7, -9, 5, -4}; int result = getArraySum(numbers, 5, &numpositive); printf("Array sum: %d\n", result); printf("Number of positive elements: %d\n", numpositive); Array sum: 2 Number of positive elements: 3 September 13, 2017 Hassan Khosravi / Geoffrey Tien

Pointers as parameters What is output after the code on the right is executed? What is on the call stack for each function call? void f1(int arg) { arg = 22; printf("f1 arg: %d\n", arg); } int x = 45; f1(x); printf("x after f1: %d\n", x); f2(&x); printf("x after f2: %d\n", x); void f2(int* arg) { *arg = 410; printf("f2 arg: %d\n", arg); } September 13, 2017 Hassan Khosravi / Geoffrey Tien

Modifying and dereferencing What is output by the following code? int main() { int a = 5; int* p = &a; // assume 0x5fbff8cc printf("value of p = %p\n", p); printf("dereferenced p = %d\n", *p); p++; printf("value of p+1 = %p\n", p); printf("dereferenced p+1 = %d\n", *p); } September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Pointer types int* p is a pointer to an integer (at some 32-bit address) char* p is a pointer to a character (also at some 32-bit address) Can we use a generic type for pointers? 01100110 00100111 11100110 01110100 p Can we dereference p without knowing the type of the variable that it is pointing to? int: 4 bytes char: 1 byte September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Generic pointers Generic pointers can be declared, but must be cast before they can be dereferenced int main() { int x = 10; char ch = 'A'; void* gp; gp = &x; printf("integer value = %d\n", *(int*)gp); // outputs 10 gp = &ch; printf("now points to char %c\n", *(char*)gp); // outputs A return 0; } September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Pointer to a pointer? int main() { int x = 5; int* p = &x; *p = 6; int** q = &p; int*** r = &q; printf("%d\n", *p); printf("%d\n", *q); printf("%d\n", *(*q)); } "You can keep adding levels of pointers until your brain explodes or the compiler melts – whichever happens soonest" stackoverflow user JeremyP September 13, 2017 Hassan Khosravi / Geoffrey Tien

Back to call-by-reference Consider the following function that adds two parameters supplied by reference int add(int* num1, int* num2) { int sum = *num1 + *num2; return sum; } int main() { int a = 2; int b = 4; int c = add(&a, &b); printf("sum = %d", c); Can we modify the add function so that it uses a pointer to return the answer? September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Returning pointers Will it work if we just change the return type to pointer, and return the sum variable's address? int add(int* num1, int* num2) { int sum = *num1 + *num2; return sum; } int main() { int a = 2; int b = 4; int c = add(&a, &b); printf("sum = %d", c); * & This will have problems! Think about what is (or was) on the call stack * * September 13, 2017 Hassan Khosravi / Geoffrey Tien

Passing array elements as parameters Arrays are passed by reference by default double getMaximum(double data[], int size); // prototype double getMaximum(double* data, int size); // equivalent prototype double answer = getMaximum(myarr, length); // function call Note that we do not need to provide "&" when specifying the address of the entire array (i.e. the address of the first element) If we want to specify the address of an individual element of the array, we would need the address operator e.g. &data[4] September 13, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Pointer arithmetic If we know the address of the first element of an array, we can compute the addresses of the other array elements (or whatever comes after, if it is meaningful) int A[5]; int* q = &A[0]; printf("q address: %p\n", q); A[0] = 2; A[1] = 4; printf("value of q: %d\n", *q); printf("value of q+1: %d\n", *(q + 1)); int x = 5; int* p = &x; printf("p address: %p\n", p); printf("value of p: %d\n", *p); printf("value of p+1: %d\n", *(p + 1)); September 13, 2017 Hassan Khosravi / Geoffrey Tien

Readings for this lesson Thareja Chapter 1.11 Next class – Dynamic memory allocation Thareja – Appendix A Lab1 take-home activity updated Deadline for Mon/Tue labs postponed September 13, 2017 Hassan Khosravi / Geoffrey Tien