Download presentation
Presentation is loading. Please wait.
Published byFranklin Barnett Modified over 8 years ago
2
Basic Concepts:-
3
Invalid use of Address Operator &75 &(‘a’) &(a+b)
4
Memory required to store Pointer Variable #include int main() { int a = 10, *ptr; ptr = &a; printf("\nSize of Integer Pointer : %d",sizeof(ptr)); return(0); } Variable may be integer,character,Float but address of the variable is always integer
5
Pointer Initialization #include int main() { int a; // Step 1 int *ptr; // Step 2 a = 10; // Step 3 ptr = &a; // Step 4 return(0); }
6
Dereferencing Pointer // Sample Code for Dereferencing of Pointer int n = 50, x ; int *ptr ; // Un-initialized Pointer ptr = &n; // Stores address of n in ptr x = *ptr; // Put Value at ptr in x // Sample Code for Dereferencing of Pointer *(ptr) = value at (ptr) = value at (2001) = 50 //Address in ptr is nothing but address of n
7
Void Pointer #include int main() { int inum = 8; float fnum = 67.7; void *ptr; ptr = &inum; printf("\nThe value of inum = %d",*((int*)ptr)); ptr = &fnum; printf("\nThe value of fnum = %f",*((float*)ptr)); return(0); }
8
Pointer to a Pointer int **ptr2ptr; int num = 45, *ptr, **ptr2ptr ; ptr = # ptr2ptr = &ptr;
9
Pointer to constant Objects int n = 30 ; const int ptr; (const int means integer is constant) ptr = &n; *ptr = 20 ;-Illegal- You can however use ptr++
10
Constant Pointers int num = 20; int * const ptr = &num ; // Declare Constant Pointer *ptr = 20 ; // Valid Statement ptr ++ ; // Invalid statement We cannot change the address stored in pointer variable.
11
Question? Is a pointer a kind of array, or is an array a kind of pointer?
12
C pointer Arithmetic Operations What is difference between? *ip +=1, ++*ip,(*ip)++ and *ip++? output? int a[] = {0,1,2,3,4}; int *p[] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++ printf(“%d %d%d\n”,ptr-p,*ptr-a,**ptr);
13
*ptr++; printf(“%d %d%d\n”,ptr-p,*ptr-a,**ptr); *++ptr; printf(“%d %d%d\n”,ptr-p,*ptr-a,**ptr); ++*ptr; printf(“%d %d%d\n”,ptr-p,*ptr-a,**ptr);
14
Array of Pointers int main() char *s[]={ “ice” “green” “cone” “please” } char **ptr[] = {s+3,s+2,s+1,s}; char ***p = ptr;
15
printf(“%s\n”,**++p); printf(“%s\n”,*--*++p+3); printf(“%s\n”,**p[-2]+3);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.