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

Pointer

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Pointer

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Pointer

Pointer
contd.
P is said to point xyz variable.
Value vs Location
Size of Basic Data type

sizeof(int) = 2
sizeof(float) = 4
sizeof(double) = 8
Sample Program
Address Operation
Pointer Arithmetic
Pointer Arithmatic Example -1
char
a[]={‘1’,’2’,’3’,’4’,’5’}
char *p;

p=a; /1000
P++ //1001
p++//1002

1001 1002 1004


1003
1000
Pointer Arithmetic - II
Pointer Arithmetic III
Array and Pointer
Array and Pointers
Passing pointer to function
Passing Pointer - Reason 1
A swap Program
Pointers and Array
When array is declared, the compiler allocates base address and sufficient
amount of memory to store all elements.

Int x[5] = {1,2,3,4,5};

X[0] X[1] X[2] X[3] X[4]

1 2 3 4 5

1000 1002 1004 1006 1008


Pointer and Array
X = &x[0] = 1000

Int *p;

p = x;

p &x[0] 1000 *p 1
p+1 &x[1] 1002 *(p+1) 2
p+2 &x[2] 1004 *(p+2) 3
p+3 &x[3] 1006 *(p+3) 4
p+4 &x[4] 1008 *(p+4) 5
Address of x[3] = base address +(3 X scale factor of int)

= 1000 +(3 X 2)

= 1006
Function returning pointer
Void main int* fun(int a,int b)
{ int a=10,b=20; {
int *x; int c;
x=fun(a,b); // fun(10,20) c=a+b;
*x++; return &c; // return 30;
print(x) }
}

You might also like