Advertisement
maycod23

Pointers

Jun 5th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void arrpass(int *a, int n)//address sedn kiya toh receive bhi pointer ne kiya
  4. {
  5. cout << a << endl;
  6. cout << *a << endl; //value at a[0]
  7. a[0] = 100; //or *a=100 is same
  8. // as the change is made at the address of base index it will reflect in the originla array also
  9. }
  10. void vectorpassbyvalue(vector<int> a)
  11. {
  12. a[0] = 100;
  13. }
  14. void vectorpassbyreference(vector<int>&a)
  15. {
  16. a[0] = 200;
  17. }
  18. int main()
  19. {
  20. /////////////////////////////////////part 1 basics of pointers
  21. // and double pointers
  22. // int i = 10;//intialise the value 10 at i
  23. // int *p = &i;//stre th adrress of i in pointer variable
  24. // cout << p << endl; //address of i
  25. // cout << *p << endl; //value of i
  26. // (*p)++; //or i++ is same but *p++==incorrect and (*p)++==correct
  27. // //after this no change in p but will change value of i
  28. // cout << i << endl;//11
  29. // // cout << *(p++) << endl;//11
  30. // // cout << (*p++) << endl;//garbage
  31.  
  32.  
  33. // int **q = &p; //q will store addrees of p
  34. // cout << q << endl; //address of p;
  35. // cout << *q << endl; //value stored at p ie address of i
  36. // cout << **q << endl; //value of i
  37.  
  38. // cout << **(q++) << endl;
  39. // // cout << (**q++) << endl;
  40. // cout << endl << endl << endl;
  41.  
  42.  
  43.  
  44. // ///////////////pointers will not point to garbage value
  45. // int a;
  46. // int *b;
  47. // cout << a << endl; //garbage at i
  48. // cout << b << endl; //garbage at pointer p
  49. // cout << *b << endl; // may throw error as pointer is having address of garbage and
  50. // //value at that garbage may or may not exist
  51. // cout << endl << endl << endl;
  52.  
  53.  
  54.  
  55. //////////////array as pointers
  56. // //arr[i],*(arr+i),i[arr];
  57. // int arr[10];
  58. // arr[0] = 5, arr[1] = 10;
  59. // cout << arr << endl;//adrres of a[0];
  60. // cout << (*arr) << endl;//value at arr[0]
  61. // cout << *(arr + 1) << endl;//value at arr[1];
  62. // cout << 0[arr] << endl;
  63. // cout << endl << endl << endl;
  64.  
  65.  
  66.  
  67. // //////////differenec in arrray and pointers
  68. // int val = 10;
  69. // int *pointer = &val;
  70. // cout << pointer << endl; //address of val
  71. // cout << *pointer << endl; //value at address of pointer
  72. // *pointer++;//etc will change val
  73. // pointer = pointer + 1; //will change the address(value) stored at pointer by 4 bytes
  74.  
  75. // int checkarr[10];
  76. // cout << checkarr << endl; //address at 0th index
  77. // cout << &checkarr[0] << endl;
  78. // cout << *(checkarr) << endl; //value at 0th index
  79.  
  80.  
  81. // //////////1st point
  82. // pointer = pointer + 1; //is valid
  83. // // checkarr = checkarr + 1; //not valid we can not reassign address in symbol table
  84. // pointer = checkarr + 1 ;// we can do this
  85. // cout << endl << endl << endl;
  86.  
  87.  
  88. /////////////character and pointers
  89. // char c1 = 'a';
  90. // char* pc = &c1;
  91. // cout << c1 << endl;
  92. // cout << pc << endl;//instead of printing address of c1 it will print c1
  93. // cout << *pc << endl;
  94.  
  95. // char str[] = "abc";
  96. // cout << str << endl;//instead of printing address of str[0],print whole string
  97. // char *pcc = &str[0];
  98. // cout << pcc << endl;
  99. // cout << *pcc << endl;
  100.  
  101.  
  102. /////////arrays and vectors pass by value and pass by reference
  103. int arraybyreference[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  104. cout << arraybyreference << endl; //address of a[0]
  105. cout << &arraybyreference[0] << endl;//address of a[0]
  106. cout << *arraybyreference << endl;//value stored at address of a[0]
  107. arrpass(arraybyreference, 10);//sending the address of a[0]
  108. for (int i = 0; i < 10; i++)
  109. {
  110. cout << arraybyreference[i] << " ";
  111. }
  112.  
  113. ////while passing array through any function we always pass address
  114. ///of its 0th index and it wil be received by pointer in function
  115. ///and change in fun will reflect in maine array
  116. ///array can only be passed via call by reference change in fun will reflect in main.
  117. cout << endl << endl << endl;
  118.  
  119.  
  120. // ////in case of vectors it can be passed in both ways;
  121. // vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  122. // vectorpassbyvalue(vec);
  123. // for (int i = 0; i < 10; i++) { cout << vec[i] << " ";} cout << endl; //no change in main vector
  124. // vectorpassbyreference(vec);
  125. // for (int i = 0; i < 10; i++) { cout << vec[i] << " ";} cout << endl; //change in main vector
  126. // cout << endl << endl << endl;
  127.  
  128.  
  129.  
  130. // ////////////reference variable
  131. // // same memory me pahucne ke do alag alag raste
  132. // //declaration of reference variable should be done at the time of initialisation
  133. // int original = 10;
  134. // int& referenceoriginal = original;
  135. // original++;
  136. // cout << original << endl;
  137. // cout << referenceoriginal << endl;
  138. // cout << endl << endl << endl;
  139.  
  140.  
  141.  
  142. // /////////dynamic memory allocation
  143. // //in variables
  144. // int *variabledynamic = new int;
  145. // cout << variabledynamic << endl;
  146. // *variabledynamic = 10;
  147. // cout << *variabledynamic << endl;
  148.  
  149. // //inarrays
  150. // int* arraydynamic = new int[10];
  151. // cout << arraydynamic << endl;
  152. // arraydynamic[0] = 100;
  153. // cout << *arraydynamic << endl;
  154. // cout << arraydynamic[0] << endl;
  155. // cout << endl << endl << endl;
  156.  
  157.  
  158.  
  159.  
  160.  
  161. }
  162.  
  163. //1 pointers in variables
  164. //2 pointers in arrays
  165. //3 pointers in char
  166. //4 pointers in char array
  167. //5 pass variable in a fun (by value or by reference)
  168. //6 pass array in a fun (only done by reference)
  169. //7 pass vector in a fun (by value or by reference)
  170. //8 double pointers
  171. //9 type casting (implicit and explicit)
  172. //10 reference variable
  173. //11 dynamic memory allocation in variables and arrays.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement