Pointers.

Slides:



Advertisements
Similar presentations
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Advertisements

Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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.
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.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CS1010 Programming Methodology
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
CSCI206 - Computer Organization & Programming
Functions and Pointers
Review of C… The basics of C scanf/printf if/elseif statements
CS1010 Programming Methodology
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Computer Science 210 Computer Organization
Pointers and References
14th September IIT Kanpur
CSC 253 Lecture 8.
CSCI206 - Computer Organization & Programming
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Lecture 18 Arrays and Pointer Arithmetic
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Structures vol2.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers.
Simulating Reference Parameters in C
Initializing variables
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Functions.
Pointers Pointers point to memory locations
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Exercise Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Pointers and dynamic objects
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CSCE 206 Lab Structured Programming in C
Pointers and References
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Pointers

Warmup Read N digits in between 0 ≤ ai ≤ 9 Find the frequency for each of the digits (rate of occurrence) Output the digits with their frequencies E.g. Number 0 occurred 1 time Number 1 occurred 12 times … Number 10 occurred 9 times Bonus: add a marking for those digits where the rate of occurrence was higher than the average. Also don’t forget to output how much the average was! 2017

What is a pointer? A pointer is a variable that stores a value (like any other) The value in this case is a memory address It points to (references) a location in computer memory Pointer data type should typically match the data it refers to Char pointer must point to a char variable Int pointer must point to an int variable Etc. Think about table of contents or apartment numbers 2017

What are they good for? Dynamic memory allocation Fast access to desired data Advanced data structures Function pointers Memory mapped IO, hardware, files etc. And more 2017

Pointer operations int *p // declare a pointer variable &var // get the location of the variable (mem address) *p // dereferencing a pointer p = &var // assigning the address of var to pointer p *p = 55 // assign a value by dereferencing a pointer printf("%p", p) // printing out the address stored in var p printf("%d", *p) // printing out the value that p points to 2017

Pointers visualized *p 0199FF8A num 25 #include <stdio.h>   int main(void) { int num = 25; int *p; p = &num; printf("%d", num); printf("%d", *p); return 0; } *p 0199FF8A num 25 0x0199FF86 0x0199FF8A 2017

Sample 1: using a pointer #include <stdio.h> int main(void) { int num; int *p; // declare a pointer variable p = &num; // assign the address of num to p scanf("%d", p); // why didn't I use & here? printf("%d\n", *p); // printing by dereferencing the pointer p *p = 55; // assigning a value using the pointer printf("%d\n", num); // which number will print? return 0; } 2017

Sample 2 #include <stdio.h> int main(void) { double pi = 3.14; double *p = π printf(“The pointer holds an address: %p\n", p); printf(“pi variable is located at:\t\t %p\n", &pi); printf("Dereferencing p we can access the value behind it\t %lg\n", *p); return 0; } 2017

Passing pointers to functions Remember about what was passed as a copy and what as original Arrays were passed as pointers to where they were declared Single variables were passed as the copy of the value it held We can however ask for the variable address and pass that We will know where the original value for the variable is held By knowing the address, we can modify the value from anywhere Variable lifetime will still limit us! 2017

Sample 3: pointers and functions #include <stdio.h> void SetVal(int *val); int main(void) { int num = 0; SetVal(&num); printf("%d\n", num); return 0; } void SetVal(int *val) *val = 55; 2017

Task 1 Download the swap.c base code Create a program that switches 2 variable values between each other Create a prototype for the function Remember to set both the return value and parameters! Write the contents for that function Write the function call Output the variable locations in both the main function and the subfunction. What’s the difference? Bonus: instead of initializing the variables, scan them in from keyboard in another function (without returning them) 2017

Pointers, arrays and pointer arithmetic Arrays are actually just pointers to the first member of the array You can use pointers to access array members You can move around an array using pointer arithmetic. This will take into account the size of the variable referred to. Arrays are passed to functions as a pointer to the start of the array Order of precedence is important! p = &array[3] // assigns the location of the fourth member of the array to p p = array // assigns the start of the array to p *(p + i) // access (dereference) the p + i member of the array p++ // increment the address by 1 element (type dependent) 2017

Pointers and arrays visualized *p 48F9AC91 int array[] = {5,3,7,3,5}; int *p; p = array; array[0] 5 array[1] 3 array[2] 7 array[3] 3 array[4] 5 *(p + 0) *(p + 1) *(p + 2) *(p + 3) *(p + 4) 2017

Sample 4: pointers and arrays #include <stdio.h>   int main(void) { int array[] = {5, 3, 7, 3, 5}; int *p = array; int i; printf("%p\n", p); printf("%p\n\n", array); for (i = 0; i < 5; i++) printf("%p, %d\n", (p + i), *(p + i)); return 0; } 2017

Task 2 (based on sample 4 code) Improve on the code so that we would get the numbers from the keyboard Find the smallest and the greatest number, output with their address in memory Output the original array for reference, include memory address for each value How much is the difference between the addresses? Use the help of pointers this time, avoid using square brackets everywhere except when declaring arrays! Bonus: Use a single function to find smallest and greatest, output findings in main 2017

Homework Find some old codes from first semester and see if you could improve on them by using pointers (e.g. Homework 1). We’ll be using pointers more and more from now through the semester. Download the ‘pointers.c’ example and try to figure it out Look at scanf function prototype, why did we need & when scanning some of the values? Try to explain to yourself how pointers work, how to get the address of a variable, how to access values using addresses? 2017