0% found this document useful (0 votes)
11 views

Lec 20 Pointers in C

Uploaded by

nasha ajawahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lec 20 Pointers in C

Uploaded by

nasha ajawahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture Outline

•Pointer?
• Declaring Pointers
• Dereferencing Operator
• Initializing Pointers
• NULL Pointer
• Increment / Decrementing a Pointer
•Pointers and Functions
• Passing Pointers to Functions
• Functions returning Pointer variables
• Safe Way to Return a valid Pointer 2
What is a Pointer?
• is a special kind of variable in which we store memory addresses
(Instead of data values).
• Suppose you want to give a prize to the winner.
• In such case you have two options:

a) Use the name of the winner- Ahmed (Call by value)


OR
b) Give this prize to the person sitting in row 5, seat NO. 8 (Call by Reference)
Location X

Address of X = 60000 X = 10;


3
General form of declaring a pointer:
 The following is a general form for declaring a pointer variable,

type_of_stored_data *pointer_variable_name;

 For example,

Char *chName;
int *nTypeOfCar;
float *fValue;

 type_of_stored_data is any valid pointer base type such as char, int, float or
other valid C derived types such as array and struct.

4
Declaring Pointers
Pointer Syntax

No space
int *myptr; // Read Right to Left
//myptr is a pointer to an integer

Similarly
int y;
int *x; // Pointing to double ; 0x00128940
x=&y;
Char *C; // Pointing to char

5
Assigning addresses to Pointers

Example:

int *ptr; // Declaration, Pointer is a whole number big enough to store memory address

int x;

x = 10;

Ptr = &x; // Assigning the address of x to the pointer “ptr”

Now in order to read the value in that memory location, we use “star operator” that is

6
Dereferencing Operator *
• *Ptr is read as the value of whatever ptr points to
Z = *ptr * 2; // 20 will be stored in Z
Initializing Pointers:
Giving starting value to a Pointer that is
Ptr = &var;
Ptr = 0;
Ptr = NULL;

// Global constant declared in the header file (#include…)


0 and Null Points to nothing
7
NULL Pointer
Null pointer is a pointer which is pointing to nothing. NULL pointer points to empty location in memory.
Value of null pointer is 0. We can make a pointer to point to Null As

Int *p = NULL;
Char *p = NULL;

NULL is generally used for pointers only.

8
Get Value of Thing Pointed by Pointers

Example:
int *pc, c;
c = 5;
pc = &c;

printf("%d", *pc); // Output: 5

Note: In the above example, pc is a pointer, not *pc. You


cannot and should not do something like *pc = &c;

9
Changing Value Pointed by Pointers

Example:
int *pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
Note: We have assigned the address of c to the pc pointer.
Then, we changed the value of c to 1. Since pc and the address of c
is the same, *pc gives us 1. 10
Declaring Pointers
int *ptr1; // one pointers on one line.

int *ptr, x; // Mix declaration

int *ptr, x, a[10]; // Declaring pointer, variable and an array


A=(&c);

NOTE: the address operator “&” can not be used in a


mathematical expression i.e; &x+1, it can only be either &X OR
&Y.
11
Common mistakes when working with pointers
Suppose, you want pointer pc to point to the address of c. Then,

int c, *pc;

// pc is address but c is not


pc = c; // Error

// &c is address but *pc is not


*pc = &c; // Error

// both &c and pc are addresses


pc = &c;

// both c and *pc are values


12
*pc = c;
Increment and Decrement Pointers

When you increment a variable, you usually add 1 to it. For example,
the value of i in the following code will be 1:

int i = 0;
i++;
Because a pointer points to an address (which is also a numeric value), you can also
increment a pointer. However, you are incrementing by address value instead of integer value.
In most C compilers, an integer is stored as 4 bytes. Therefore, if your integer pointer has a
value of 62fe30, incrementing the pointer will result in a new address of 62fe34.

Note that memory addresses are stored as hexadecimal values. The decimal value of 62fe34 is
6487604. 13
Incrementing the Address of Pointer
The following code loops through an array, incrementing the pointer along the way. In the for
loop, it displays the address (ptr) and the value (*ptr):

#include <stdio.h> Output:


int main(void)
{ Address of scores[0] array = c8264abc
int scores[] = {1500, 1700, 2250}; Value stored at scores[0] = 1500
int i = 0;
int *ptr = NULL;
Address of scores[1] array = c8264ac0
ptr = &scores;
for(i=0;i<3;i++) {
Value stored at scores[1] = 1700
printf("Address of scores[%d] array = %x\n", i,
ptr); Address of scores[2] array = c8264ac4
printf("Value stored at scores[%d] = %d\n", i, Value stored at scores[2] = 2250
*ptr);
/* increment */
ptr++;
}
} 14
Incrementing the Value of Pointer in an Array (Example)

Because a pointer points to another value (e.g., an address in memory), it is also useful for
array processing. We can indicate the bucket of an array by using its index; but we can also
use pointers. For example, look at the following array and how the incrementing works.
#include <stdio.h>
int main(void) Output:
{
int scores[5] = {100, 235, 275, 50, 100};
Value stored in pointer after increment is: 235
int *ptr = NULL;
ptr = &scores;

printf("Value stored in pointer after increment is: %d", *++ptr);

In this case we use increment operator before the pointer variable like ++ptr, and use the asterisk (*) to point
to the value. When you run this, it will indicate the second index, because we have incremented by one.15
Examples
Example 1.
int *pc, c; Explanation 1.
c = 5; We have assigned the address of c to the pc pointer.
pc = &c;
*pc = 1; Then, we changed *pc to 1 using *pc = 1;. Since pc and
printf("%d", *pc); // Ouptut: 1 the address of c is the same, c will be equal to 1.
printf("%d", c); // Output: 1

Example 2.
int *pc, c, d;
c = 5; Explanation 2.
d = -15; Initially, the address of c is assigned to the pc pointer
pc = &c; using pc = &c;. Since c is 5, *pc gives us 5.
printf("%d", *pc); // Output: 5
pc = &d; Then, the address of d is assigned to the pc pointer using
printf("%d", *pc); // Ouptut: -15 pc = &d;. Since d is -15, *pc gives us -15.
16
Example
#include <stdio.h>
int main()
{
int *pc, c;

c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22

pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
17
Example

*pc = 2;
Output
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2 Address of c: 2686784
Value of c: 22

return 0; Address of pointer pc: 2686784


} Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

18
Passing Pointer to a Function (Revision)

When we pass a pointer as an argument instead of a variable then the


address of the variable is passed instead of the value. So any change made
by the function using the pointer is permanently made at the address of
passed variable.

Pointer as a function parameter is used to hold addresses of arguments


passed during function call. This is also known as call by reference. When a
function is called by reference any change made to the reference variable
will effect the original variable.

19
Example Time: Swapping two numbers using Pointer

#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);
swap(&m, &n);//passing address of m and n to the swap
function

printf("After Swapping:\n\n");
printf("m = %d\n", m);
printf("n = %d", n);
return 0;
} 20
Example Time: Swapping two numbers using Pointer

/*
pointer 'a' and 'b' holds and
points to the address of 'm' and 'n‘*/

void swap(int *a, int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp; Output
m = 10
} n = 20
After Swapping:
m = 20
n = 10
21
Functions returning Pointer variables

A function can also return a pointer to the calling function. In this case you must be careful,
because local variables of function doesn't live outside the function. They have scope only
inside the function. Hence if you return a pointer connected to a local variable, that pointer
will be pointing to nothing when the function ends.

#include <stdio.h>
int *larger(int*, int*);
void main()
{
int a = 15;
int b = 92;
int *p;
p = larger(&a, &b);
22
Functions returning Pointer variables

printf("%d is larger",*p);
}
int *larger(int *x, int *y)
{
if(*x > *y)
return x;
else
return y;
}
OutPut

92 is larger

23
Safe Way to Return a valid Pointer

• Either use arguments with functions, Because argument passed to the function are declared
inside the calling function, hence they will live outside the function as well.

OR,

• use static local variables inside the function and return them. As static variable has lifetime
until the main() exists Therefore they will be available through out the program.

24
Summery
• Pointer?
• Declaring Pointers
• Dereferencing Operator
• Initializing Pointers
• NULL Pointer
• Increment / Decrementing a Pointer
• Pointers and Functions
• Pointer to a function
• Declaring Pointer to a function
• Assigning address of function to a Pointer
• Calling mechanism of pointer to function
• Passing Pointers to Functions
• Functions returning Pointer variables
• Safe Way to Return a valid Pointer
25
26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy