Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void arrpass(int *a, int n)//address sedn kiya toh receive bhi pointer ne kiya
- {
- cout << a << endl;
- cout << *a << endl; //value at a[0]
- a[0] = 100; //or *a=100 is same
- // as the change is made at the address of base index it will reflect in the originla array also
- }
- void vectorpassbyvalue(vector<int> a)
- {
- a[0] = 100;
- }
- void vectorpassbyreference(vector<int>&a)
- {
- a[0] = 200;
- }
- int main()
- {
- /////////////////////////////////////part 1 basics of pointers
- // and double pointers
- // int i = 10;//intialise the value 10 at i
- // int *p = &i;//stre th adrress of i in pointer variable
- // cout << p << endl; //address of i
- // cout << *p << endl; //value of i
- // (*p)++; //or i++ is same but *p++==incorrect and (*p)++==correct
- // //after this no change in p but will change value of i
- // cout << i << endl;//11
- // // cout << *(p++) << endl;//11
- // // cout << (*p++) << endl;//garbage
- // int **q = &p; //q will store addrees of p
- // cout << q << endl; //address of p;
- // cout << *q << endl; //value stored at p ie address of i
- // cout << **q << endl; //value of i
- // cout << **(q++) << endl;
- // // cout << (**q++) << endl;
- // cout << endl << endl << endl;
- // ///////////////pointers will not point to garbage value
- // int a;
- // int *b;
- // cout << a << endl; //garbage at i
- // cout << b << endl; //garbage at pointer p
- // cout << *b << endl; // may throw error as pointer is having address of garbage and
- // //value at that garbage may or may not exist
- // cout << endl << endl << endl;
- //////////////array as pointers
- // //arr[i],*(arr+i),i[arr];
- // int arr[10];
- // arr[0] = 5, arr[1] = 10;
- // cout << arr << endl;//adrres of a[0];
- // cout << (*arr) << endl;//value at arr[0]
- // cout << *(arr + 1) << endl;//value at arr[1];
- // cout << 0[arr] << endl;
- // cout << endl << endl << endl;
- // //////////differenec in arrray and pointers
- // int val = 10;
- // int *pointer = &val;
- // cout << pointer << endl; //address of val
- // cout << *pointer << endl; //value at address of pointer
- // *pointer++;//etc will change val
- // pointer = pointer + 1; //will change the address(value) stored at pointer by 4 bytes
- // int checkarr[10];
- // cout << checkarr << endl; //address at 0th index
- // cout << &checkarr[0] << endl;
- // cout << *(checkarr) << endl; //value at 0th index
- // //////////1st point
- // pointer = pointer + 1; //is valid
- // // checkarr = checkarr + 1; //not valid we can not reassign address in symbol table
- // pointer = checkarr + 1 ;// we can do this
- // cout << endl << endl << endl;
- /////////////character and pointers
- // char c1 = 'a';
- // char* pc = &c1;
- // cout << c1 << endl;
- // cout << pc << endl;//instead of printing address of c1 it will print c1
- // cout << *pc << endl;
- // char str[] = "abc";
- // cout << str << endl;//instead of printing address of str[0],print whole string
- // char *pcc = &str[0];
- // cout << pcc << endl;
- // cout << *pcc << endl;
- /////////arrays and vectors pass by value and pass by reference
- int arraybyreference[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- cout << arraybyreference << endl; //address of a[0]
- cout << &arraybyreference[0] << endl;//address of a[0]
- cout << *arraybyreference << endl;//value stored at address of a[0]
- arrpass(arraybyreference, 10);//sending the address of a[0]
- for (int i = 0; i < 10; i++)
- {
- cout << arraybyreference[i] << " ";
- }
- ////while passing array through any function we always pass address
- ///of its 0th index and it wil be received by pointer in function
- ///and change in fun will reflect in maine array
- ///array can only be passed via call by reference change in fun will reflect in main.
- cout << endl << endl << endl;
- // ////in case of vectors it can be passed in both ways;
- // vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- // vectorpassbyvalue(vec);
- // for (int i = 0; i < 10; i++) { cout << vec[i] << " ";} cout << endl; //no change in main vector
- // vectorpassbyreference(vec);
- // for (int i = 0; i < 10; i++) { cout << vec[i] << " ";} cout << endl; //change in main vector
- // cout << endl << endl << endl;
- // ////////////reference variable
- // // same memory me pahucne ke do alag alag raste
- // //declaration of reference variable should be done at the time of initialisation
- // int original = 10;
- // int& referenceoriginal = original;
- // original++;
- // cout << original << endl;
- // cout << referenceoriginal << endl;
- // cout << endl << endl << endl;
- // /////////dynamic memory allocation
- // //in variables
- // int *variabledynamic = new int;
- // cout << variabledynamic << endl;
- // *variabledynamic = 10;
- // cout << *variabledynamic << endl;
- // //inarrays
- // int* arraydynamic = new int[10];
- // cout << arraydynamic << endl;
- // arraydynamic[0] = 100;
- // cout << *arraydynamic << endl;
- // cout << arraydynamic[0] << endl;
- // cout << endl << endl << endl;
- }
- //1 pointers in variables
- //2 pointers in arrays
- //3 pointers in char
- //4 pointers in char array
- //5 pass variable in a fun (by value or by reference)
- //6 pass array in a fun (only done by reference)
- //7 pass vector in a fun (by value or by reference)
- //8 double pointers
- //9 type casting (implicit and explicit)
- //10 reference variable
- //11 dynamic memory allocation in variables and arrays.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement