Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Problem Solving and Programming

Similar presentations


Presentation on theme: "Introduction to Problem Solving and Programming"— Presentation transcript:

1 Introduction to Problem Solving and Programming
Lecture 9 – Pointers

2 Addresses Each user-defined variable is stored at a location in the memory and has a value. The location is uniquely identified by an address. The address is retrieved by the “&” sign.

3 Pointers A pointer is a variable that “points” to the block of memory that another variable represents. In other words, it stores the memory address of another variable. The other variable may be of type int, char, array, structure, function or any other.

4 Variables Normal variables Pointer variables
Contain a specific value (direct reference) Example : count Pointer variables Contain memory addresses as their values Example: countPtr count 7 count 7 countPtr

5 Pointer Variable Declarations
Pointer declarations Declaration: data_type *pointer_name; ‘*’ used with pointer variables int *myPtr; float *Ptr; Char *strPtr; Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2;

6 Pointer Variable Initialization
Initialize pointers to: 0 or NULL, points to nothing (NULL preferred) Example: int *yPtr; an address of a variable points to an address of a user defined variable in the memory Example: int y = 5; int *yPtr; yPtr = &y; an address (absolute value) points to a certain place/address in the memory Example: int *ptr=(int *)1000;

7 Variables vs Pointer Variables
int a=5; int * ptr; ptr=&a; About variable a: About variable ptr: 1. Name of variable : a 4. Name of variable: ptr 2. Value of variable: 5 5. Value of variable: 1025 3. Address: 1025 (assume) 6. Address: 5000 (assume)

8 Dereferencing = Using Addresses
Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator. Given pointer ptr, to get value at that address, write *ptr after the declaration. Example: int x = 5; int *ptr = &x; *ptr = 6; /* Access x via ptr, and changes it to 6, equivalent to x = 6 */ printf(“%d”, x); // Will print 6 now

9 Example

10 Example (cont.) - output

11 Pointer Arithmetic Can do math on pointers Example: char* ptr;
ptr+i = ptr + i * sizeof(data_type of ptr) ptr ptr + 1 a b c d

12 Data type sizes

13 Pointer Arithmetic Arithmetic operations can be performed on pointers
Increment/decrement pointer (++ or --) Add an integer to a pointer ( + or += , - or -=) Pointers may be subtracted from each other Operations on a single pointer are meaningless unless performed on an array because it will access unrelated variables randomly.

14 Pointer Expressions and Arithmetic
Pointer comparison ( <, == , > ) See which pointer points to the higher numbered array element (index), subsequently higher address Also, checks if a pointer points to nothing

15 Pointer Arithmetic-Example
#include<stdio.h> int main() { int *ptr=( int *)1000; ptr=ptr+1; printf(" %d",ptr); return 0; } Output 1004

16 Pointer Arithmetic-Example
#include<stdio.h> int main() { double *p=(double *)1000; p=p+3; printf(" %d",p); return 0; } Output 1024

17 Pointer Arithmetic-Example
#include<stdio.h> int main() { int *p=(int *)1000; int *temp; temp=p; p=p+2; printf("%d %d\n",temp,p); printf("difference= %d",p-temp); return 0; } Output Difference= 2

18 Pointer Arithmetic-Example
#include<stdio.h> int main() { float *p=(float *)1000; float *q=(float *)2000; printf("Difference= %d",q-p); return 0; } Output Difference= 250

19 Pointers & Arrays Array variables are pointers to the array start
Example: char *ptr; char str[10]; ptr = str; //ptr now points to array start ptr = &str[0]; //Same as above line Array indexing is same as dereferencing after pointer addition. str[1] = ‘a’; *(ptr+1) = ‘a’;// same as above line

20 Pointers & Arrays

21 a is a pointer only to the first element—not the whole array.
Arrays and Pointers Note same a &a[0] a is a pointer only to the first element—not the whole array.

22 Arrays and Pointers

23 Arrays and Pointers

24 Arrays and Pointers Note
To access an array, any pointer to the first element can be used instead of the name of the array.

25 Pointer Expressions (Recap)
operation Description p++, p-- Increment (decrement) p to point to the next element, it is equivalent to p+=1 (p -=1) p+i , p-i Point to i-th element beyond (in front of) p but value of p is fixed p[i] Equivalent to p + i if p points to the start of array p + n (integer) n must be an integer, its meaning is offset p - q Offset between pointer p and pointer q p+q, p*q, p/q, p%q invalid Relational operator of two pointers p, q valid, including p > q, p < q, p == q, p >= q, p <= q, p != q

26 Pointers & Arrays-Example
#include <stdio.h> int main(void) { int i; int my_array[] = {1,23,17,4,-5,100}; //Compiler figures out size int *ptr; ptr = &my_array[0]; /* point our pointer to the first element of the array */ printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); } return 0;

27 Array of Pointers Arrays can contain pointers to …..

28 Array of Pointers int *z[4] = {NULL, NULL, NULL, NULL};
int a[4] = {1, 2, 3, 4}; z[0] = a; // same as &a[0]; z[1] = a + 1; // same as &a[1]; z[2] = a + 2; // same as &a[2]; z[3] = a + 3; // same as &a[3]; for (int x=0;x<4;x++) printf("\n %d --- %d ",a[x],*z[x]);

29 for (int x=0;x<4;x++)
printf("\n %d --- %d ",a[x], z[x]);

30 Array of Pointers Arrays can contain pointers to (array)

31 Array of Pointers int x[] = {32, 18, 12, 24};
int y[] = {13, 11, 16, 12, 42, 19, 14}; int *z[2] = {NULL, NULL}; int i; z[0] = x; // same as &x[0]; z[1] = y; // same as &y[0]; for (i=0;i<2;i++) printf("\n %d “,*z[i]);

32 Dynamic Allocation #include <stdlib.h> sizeof
returns number of bytes of a data type. malloc finds a specified amount of free memory and returns a void pointer to it. ▫ char * str = (char *) malloc( 3 * sizeof(char) );

33 Dynamic DeAllocation #include <stdlib.h> free
declares the memory pointed to by a pointer variable as free for future use: char * str = (char *) malloc( 3 *sizeof(char)); ... use str ... free(str);

34 Dynamically Allocated Arrays
Allows you to avoid declaring array size at� declaration.� Use malloc to allocate memory for array when needed: Example: int *dynamic_array; dynamic_array = malloc( sizeof( int ) * 10 ); dynamic_array[0]=23; /* initialize value at the start of array to 23 */

35 Example

36 Questions?


Download ppt "Introduction to Problem Solving and Programming"

Similar presentations


Ads by Google