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

Multiple Pointers To A Variable

The document discusses pointers in C programming. It shows how multiple pointers can point to the same variable, how to declare and initialize pointer variables, access variables through pointers, perform pointer arithmetic, and use pointers to add two numbers. Key points covered include declaring pointer types, dereferencing pointers, pointer assignment, and passing pointers to functions.

Uploaded by

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

Multiple Pointers To A Variable

The document discusses pointers in C programming. It shows how multiple pointers can point to the same variable, how to declare and initialize pointer variables, access variables through pointers, perform pointer arithmetic, and use pointers to add two numbers. Key points covered include declaring pointer types, dereferencing pointers, pointer assignment, and passing pointers to functions.

Uploaded by

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

Multiple Pointers to a Variable

Value of ptr1
ptr1= &a Address of a
a
&a
14650 -123
ptr1
ptr2= &a

ptr2 14650

Value of ptr2
Address of a
Pointer Constants

Note

A pointer that points to no variable contains the

special null-pointer constant, NULL.


Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4;
ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3;
ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
ptr1 7 *p = 8
ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
ptr1 7 *ptr1 = 8 ptr1 8
ptr2 x ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
ptr1 7 *p = 8 ptr1 8
ptr2 x ptr2 x
ptr1 8 *&x = *ptr2 + *ptr1;
pt2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
ptr1 7 *p = 8 ptr1 8
ptr2 x ptr2 x
ptr1 8 *&x = *ptr2 + *ptr1; ptr1 16
pt2 x pt2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 x
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 x
ptr1 7 *p = 8 ptr1 8
ptr2 x ptr2 x
ptr1 8 *&x = *ptr2 + *ptr1; ptr1 16
pt2 x pt2 x
ptr1 16 x = *ptr1 * *ptr2
ptr2 x
Accessing Variables Through Pointers

Before Statement After

ptr1 ? x = 4; ptr1 4
ptr2 x ptr2 X
ptr1 4 x = x+3; ptr1 7
ptr2 x ptr2 X
ptr1 7 *p = 8 ptr1 8
ptr2 x ptr2 X
ptr1 8 *&x = *ptr2 + *ptr1; ptr1 16
pt2 x pt2 X
ptr1 16 x = *ptr1 * *ptr2 ptr1 256
ptr2 x ptr2 x
Pointer Variable Declaration

data declaration

type
type Identifier or variable

pointer declaration

type
type ** Identifier or variable
Declaring Pointer Variables

char a; Z char* p;
int n; 15 Int* q;
float x; 3.3 float* r;
Pointers - Example
#include <stdio.h>
int main ( ) a 14
{
int a;
int* p;

a = 14;
p = &a;
p 135760
printf(“%d %p \n”, a, &a);
printf(“%p %d %d”, p,*p, a);
}
Output:
14 00135760
00135760 14 14
Uninitialized Pointers
Unknown
value

int a; a ??? ?
Int* p; p ???

Pointer to
Unknown
location
Initializing Pointer Variables

int * p;
declaration

int* p = &a;

p = &a;
initialization
Fun with Pointers
#include <stdio.h>
int main( )
{
int a,b,c; b c
a
int* p;
int* q;
p q r
int* r;

a=6;
b=2; a 6 b 2 c
p=&b;
p q r
Cont…
q=p;
r=&c;

P=&a;
*q=8;

*r=*p;

*r=a+*q+*&c;
printf(“%d %d %d \n”,a,b,c);
printf(“%d %d %d”, *p, *q, *r);

Output:
6 8 20
6 8 20
Pointer – Example - Add two numbers using
pointers
#include <stdio.h>

void main( )
{
int a,b,c;
int* pa = &a;
int *pb = &b;
int* pc = &c;
printf(“Enter the first number: “);
scanf(“%d”, pa);
printf(“Enter the second number: “);
scanf(“%d”, pb); Output:
*pc = *pa+ *pb; Enter the First number: 10
printf(“\n%d + %d is %d”, *pa, *pb, *pc); Enter the second number: 15
}
10 + 15 is 25

You might also like