Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers.

Similar presentations


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

1 Pointers

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

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? 2017


Download ppt "Pointers."

Similar presentations


Ads by Google