Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers.

Similar presentations


Presentation on theme: "Pointers."— Presentation transcript:

1 Pointers

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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


Download ppt "Pointers."

Similar presentations


Ads by Google