pointer basics
TRANSCRIPT
![Page 1: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/1.jpg)
Prepared by Mohammed SikanderTechnical LeadCranes Software International Limited
![Page 2: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/2.jpg)
int main( ){
int x = 10;printf(“x = %d “ , x); //Prints the value of x.printf(“&x = %p “ , &x);
}
& operator gets the address of variable.%p is the correct format specifier to print address.
![Page 3: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/3.jpg)
1. int main( )2. {3. int x = 10;4. printf(“x = %d \n“ , x); //Prints the value of x.5. printf(“&x = %p \n“ , &x);6. int *ptr; //Declaration of pointer7. ptr = &x;8. printf(“ptr = %p \n” , ptr);9. printf(“*ptr = %d \n” , *ptr);10. }
6. * is used to declare a pointer9. * is used to get value at address (dereference)
![Page 4: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/4.jpg)
1. int main( )2. {3. int x = 5, y = 8;4. int *p1 , p2;5. p1 = &x;6. p2 = &y;7. printf(“ *p1 = %d “ , *p1);8. printf(“ *p2 = %d “ , *p2);9. }
![Page 6: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/6.jpg)
Pointer size is same irrespective of type.
Pointers are like shorcuts to files (in windows).A size of shortcut to a 1MB file, 1GB file , 200MB file is same
![Page 7: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/7.jpg)
![Page 9: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/9.jpg)
int main( ){
int x;printf(“ x = %d “ , x); //garbage value.int *ptr;printf(“ ptr = %p “ , ptr); //garbage address.printf(“ *ptr = %d “ ,*ptr);
}
![Page 10: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/10.jpg)
int main( ){
int *ptr1; // unitialized pointerint *ptr2 = NULL; // NULL pointer
if(ptr2 != NULL) // Can be safeguardedprintf(“ *ptr2 = %d \n”, *ptr2);
if(ptr1 != NULL) // Cannot be safeguardedprintf(“ *ptr1 = %d \n”, *ptr1);
}
![Page 11: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/11.jpg)
void update(int x){
x = x + 5;printf(“Update x = %d “ , x);
}int main( ){
int x = 2;update( x );printf(“Main x = %d “ , x);
}
![Page 12: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/12.jpg)
void update(int *px){
*px = *px + 5;printf(“Update *px = %d “ , *px);
}int main( ){
int x = 2;update( &x );printf(“Main x = %d “ , x);
}
![Page 15: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/15.jpg)
a = 3 , b = 7a = a + b; //10b = a – b; //3a = a – b; //7
a = 7 , b = 10a = a * b; //70b = a / b; //7a = a / b; //10
a = 5 , b = 10a = a ^ b; //b = a ^ b; //5a = a ^ b; //10
![Page 16: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/16.jpg)
int main( ){
int arr[ ] = {12,23,34,54};printf(“ %p “ , &arr[0]);printf(“ %p “ , arr);
}
Array name gives the base address (address of first element of array )
![Page 17: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/17.jpg)
int main( ){
int arr[ ] = {12,23,34,54};int *p1 = &arr[0];int *p2 = arr;
printf(“ %d “ , *p1);printf(“ %d “ , *p2);
}
![Page 18: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/18.jpg)
The following arithmetic operations with pointers are legal:• add an integer to a pointer (+ and +=)• subtract an integer from a pointer(- and -=).• use a pointer as an operand to the ++ and -- operators.• subtract one pointer from another pointer, if they point
to objects of the same type.• compare two pointers• Operations meaningless unless performed on an array
• All other arithmetic operations with pointers are illegal.
![Page 19: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/19.jpg)
Ptr = ptr + int When you add or subtract an integer to or from a
pointer, the compiler automatically scales the integer to the pointer's type. In this way, the integer always represents the number of objects to jump, not the number of bytes.
int x; // Assume address of x is 1000 int *ptr = &x; // ptr = 1000 ptr = ptr +1; // ptr-> 1004 and not 1001
![Page 20: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/20.jpg)
int main( ){
int arr[ ] = {0x21343782 , 0x54562319};int *p1 = &arr[0];printf(“ &arr[0] = %p \n” , &arr[0]); //100printf(“ &arr[1] = %p \n” , &arr[1]); //104
printf(“ p1 = %p *p1 = %x” , p1 , *p1); //100p1++;printf(“ p1 = %p *p1 = %x” , p1 , *p1); //104
}
![Page 21: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/21.jpg)
int main( ){
short int arr[ ] = {0x2134 , 0x5456};short int *p1 = &arr[0];printf(“ &arr[0] = %p \n” , &arr[0]); //100printf(“ &arr[1] = %p \n” , &arr[1]); //102
printf(“ p1 = %p *p1 = %x” , p1 , *p1); //100p1++;printf(“ p1 = %p *p1 = %x” , p1 , *p1); //102
}
![Page 22: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/22.jpg)
int main( ){
int arr[ ] = {0x21343782 , 0x54562319};short int *p1 = &arr[0];printf(“ *p1 = %x \n” ,*p1);p1++;printf(“ *p1 = %x \n” ,*p1);
}
![Page 23: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/23.jpg)
int main( ){
int arr[ 5 ] = {12,23,34,45,56};int *ptr = arr;for(int i = 0 ; i < 5 ; i++){
printf(“ %d “ , *ptr);ptr++;
}}
![Page 24: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/24.jpg)
int main( ){
int arr[ 5 ] = {12,23,34,45,56};int *ptr = arr;for(int i = 0 ; i < 5 ; i++){
printf(“ %d “ , ptr[ i ]);}
}
![Page 25: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/25.jpg)
int main( ){
int arr[ 5 ] = {12,23,34,45,56};for(int i = 0 ; i < 5 ; i++){
printf(“ %d “ , *(arr + i));}
}
![Page 26: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/26.jpg)
void printArray(int arr[]){
printf(" %d " , sizeof(arr));
}
int main( ){
int arr[5] = {12,23,34,54,67};
printf(" %d " , sizeof(arr));
printArray( arr );}
![Page 27: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/27.jpg)
void printArray(int arr[]){
arr++; //No Error
}
int main( ){
int arr[5] = {12,23,34,54,67};
// arr++; //ErrorprintArray( arr );
}
![Page 29: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/29.jpg)
int main( ){
const int x= 5;
x = 20; //ERRORx++; //ERROR
}
int main( ){1. const int x= 5;2. int *ptr = &x;3. *ptr = 20;4. printf(“ %d “ , x);}
![Page 30: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/30.jpg)
int main( ){1. const int x= 5;2. const int *ptr = &x;3. *ptr = 20;4. printf(“ %d “ , x);}
![Page 31: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/31.jpg)
Constant Pointer : Pointer is fixed to one location.
Pointer to const : Pointer has read-only access.
int * const cp; const int * pc; Constant pointer should be initialized.
![Page 32: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/32.jpg)
int main( ){
int x = 10 ;const int y = 20;const int * pc1 ;pc1 = &x;pc1 = &y;
}
int main( ){
int x = 10 ;const int y = 20;int * const cp1;int * const cp2 = &x;int * const cp3 = &y;const int * const cp4 = &y;cp2 = &y;
}
![Page 33: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/33.jpg)
size_t strlen(const char *str);
size_t mystrlen(const char *str){
const char *end = str;while( *end != ‘\0’)
end++;return end – str;
}
![Page 34: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/34.jpg)
char * strcpy(char *dest, const char *src);
void mystrcpy(char *dest, const char *src){
while(1){
*dest = *src;if(*dest == ‘\0’)
return;src++;dest++;
}}
![Page 35: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/35.jpg)
char * strcpy(char *dest, const char *src);
void mystrcpy(char *dest, const char *src){
while((*dest++ = *src++)){}
}
![Page 36: Pointer basics](https://reader031.vdocuments.us/reader031/viewer/2022021421/5a661cc57f8b9a214f8b56eb/html5/thumbnails/36.jpg)
int strcmp(const char *str1 , const char *str2);
int mystrcmp(const char *str1 , const char *str2){
while(1){
if(*str1 != *str2)return *str1 - *str2;
if(*str1 == ‘\0’)return 0;
str1++;str2++;
}}
while(*str1 == *str2 && *str1 != ‘\0’){
str1++;str2++;
}return *str1 - *str2;