Last active
November 28, 2019 20:34
-
-
Save winhtut/deedb2c2aa8db2154dd5ff9083a811fd to your computer and use it in GitHub Desktop.
PointerLessonByWinHtut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main() | |
| { | |
| int arr[5]={10,30,20,50,60}; | |
| int *ptr1=&arr[0];// *ptr=arr; | |
| int *ptr2= ptr1+3; | |
| printf(" address of ptr1= %d\n",ptr1); | |
| //pointer address = p + x*sizeof(*p); | |
| // = p + 3 * 4 ; | |
| // = 6356704 + 12 ; | |
| // = 6356716;>> which memory address? | |
| printf(" address of index 1 : %d\n",&arr[1]); | |
| printf(" address of index 2 : %d\n",&arr[2]); | |
| printf(" address of index 3 : %d\n",&arr[3]); | |
| printf(" address of index 4 : %d\n",&arr[4]); | |
| printf(" value is %d \n",*ptr2); // output 50 ; | |
| int *ptr3; | |
| ptr3= ptr2-ptr1;// 6356716- 6356704 = 12 / 4 = 3 | |
| printf(" ptr2-ptr1 = %d",ptr3); | |
| return 10; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //pointer type-casting int=> char , int=>float | |
| #include<stdio.h> | |
| int main() | |
| { | |
| int arr[] = {10, 20, 30, 40, 50, 60};//5*4 = 20 | |
| int *ptr1 = arr; | |
| int *ptr2 = arr + 5; | |
| printf("Number of elements between two pointer are: %d.\n", | |
| (ptr2 - ptr1)); | |
| printf("number of bytes between two pointers are: %d", | |
| (double*)ptr2-(double*)ptr1); | |
| // 1 bytes = 20/1=20 ; for char | |
| // 1 bytes = 20/4=5 ; for float | |
| // 1 bytes = 20/8=2 ; for double | |
| //first way | |
| //second way | |
| // x*sizeof(oragi)/sizeof(new); | |
| //= 5 *4 / 1 = 20 | |
| return 0; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| void fun(int arr[],int sz){ | |
| int i=0; | |
| //printf("the size of first index arr[] %d\n",sizeof(arr)); | |
| //int arr_size=sizeof(arr)/sizeof(arr[0]); | |
| //printf("arr size is %d\n",arr_size); | |
| for(i=0; i<sz;i++){ | |
| printf("the value is %d\n",arr[i]); | |
| } | |
| } | |
| int main(){ | |
| int arr[4]={10,20,30,40}; | |
| size_t sz=sizeof(arr)/sizeof(arr[0]); | |
| printf("the size of our array is :%d\n",sz); | |
| fun(arr,sz); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment