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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
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.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
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:
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
CS1010 Programming Methodology
CS1010 Programming Methodology
User-Written Functions
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
CS1010 Programming Methodology
C Programming Tutorial – Part I
CS1010 Discussion Group 11 Week 9 – Pointers.
CS1010 Programming Methodology
CS1010 Programming Methodology
Pointers.
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
POINTERS.
Programmazione I a.a. 2017/2018.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Computer Science 210 Computer Organization
Arrays, For loop While loop Do while loop
Object Oriented Programming COP3330 / CGS5409
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
A function with one argument
Structures vol2.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Revision.
Objectives You should be able to describe: Addresses and Pointers
Files.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Functions.
Pointers Pointers point to memory locations
String manipulation string.h library
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Exercise Arrays.
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Pointers and dynamic objects
CSCE 206 Lab Structured Programming in C
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to Pointers
Presentation transcript:

Pointers

Warmup (similar to last week) 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! 2018 Risto Heinsar

What is a pointer? A pointer is a memory address, it refers/points to a location in computer memory A pointer variable can hold that address It’s a data type, just as any other A pointer variable has a type that matches what it’s pointing at int *pList; char *pKey; void *pMember; Think about table of contents or apartment numbers They are typically shown as hexadecimal numbers (0x0, 0x0fff1a9b) 2018 Risto Heinsar

What are they good for? Lower level hardware access (operating systems, drivers) Abstractions Dynamic memory allocation Direct access to desired data Advanced data structures (trees, linked lists, queues, …) Function pointers Memory mapped IO, hardware, files etc. Etc. 2018 Risto Heinsar

NULL pointer A pointer that does not refer to a valid object Behavior undefined on access (e.g. segmentation fault) Often used to indicate pointers not in use or failed operations End of a list (advanced data structures) Memory allocation failed (dynamic memory management) Opening file failed Search for substring failed (strstr from string.h) Assigning pointer to NULL avoids unintentional access to released resources NULL is a pointer, 0 is an integer. NULL pointer may not always be zero, but typically is. 2018 Risto Heinsar

Pointer operations int *p // declare a pointer variable &var // get the location of the variable (memory 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 2018 Risto Heinsar

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 2018 Risto Heinsar

Sample 1: pointer and its address #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; } 2018 Risto Heinsar

Sample 2: pointers for scanf #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; } 2018 Risto Heinsar

Passing pointers to functions Remember about what was passed as a copy and what as original Single variables are passed as the copy of the value it held Arrays are passed as pointers to the first element of that array in the location it was declared We can choose to pass the location (pointer) of any variable Instead of returning a value, we can set multiple values using pointers Don’t forget, that each function still has it’s own memory Variable lifetime will still limit us! (scope) 2018 Risto Heinsar

Sample 3: pointers and functions #include <stdio.h> void SetVal(int *val); int main(void) { int num = 0; SetVal(&num); // passing the location of the variable num printf("%d\n", num); return 0; } void SetVal(int *val) // this parameter is now a pointer, as we are passing an address *val = 55; // assigning a value using dereferencing 2018 Risto Heinsar

Task 1 Download the swap.c base code Create a program that reads two numbers and swaps them Create two functions to both read and swap the variable values. Both of these must have void return type! Write the prototypes for these functions Call those functions in the appropriate order Function comments will help you Output the variable value locations in both the main function and the created functions. What’s the difference? 2018 Risto Heinsar

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. 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) 2018 Risto Heinsar

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) 2018 Risto Heinsar

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; } 2018 Risto Heinsar

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? 2018 Risto Heinsar

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 2018 Risto Heinsar

Task 2 part 1/2 (parts are graded separately) Declare an array in main() that can hold 10 integers Use a smaller array or stream redirection for testing! Later change the constant when presenting Create the following functions Function that reads all of the array members from the keyboard Function that outputs the values and their corresponding memory addresses Function that finds the minimum and maximum number from that array at the same time. The results of this must be printed out in main(). You’re not allowed to use square brackets for array indexing this time! 2018 Risto Heinsar

Task 2 part 2/2 (parts are graded separately) Create the following function that has the capability to print out n numbers from that array. It can only have two parameters. Call this function repeatedly so that: 1. time it will print all of the members of that array 2. time it will print elements from 0 to 4 3. time it will print elements 3 – 9 You can combine this function with the one that you created as the second function from the previous slide 2018 Risto Heinsar