Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Pointers:-Variables That Stores Address of another variable
- #include<iostream>
- using namespace std;
- int main()
- {
- int a = 5;
- int* p; //p is pointer to integer
- p = &a;// p store address of a
- //cout << *p<<endl; //* this called derefrencing
- // Pointer Airthmetic
- int b = 10;
- int* ptr = &b;
- //cout << ptr<<endl;//address of b
- //cout << ptr + 1 << endl;//address of b+1 means 4bytes aage ka address par jaye ga
- //Void Pointer
- void* po;
- po = &b; //we can't dereference or pointer Airthmeic
- //Pointer To Pointer
- int x = 5;
- int* pt = &x;//pointer to integer
- *pt = 6;
- int** q =& pt;//pointer to pointer to integer
- int*** r = &q;//pointer to pointer to poiner to integer
- cout << *pt << endl;//6
- cout << *q << endl;//Address of x
- cout << &x << endl;//Address of x
- cout << *(*q) << endl;//6
- cout << *(*r) << endl;//Address of x
- cout << *(*(*r)) << endl;//Value of x 6
- cout << r << endl;//Address of q
- cout << &q << endl;//Address of q
- cout << *r << endl;//Address of pt
- cout << &pt << endl;//Address of pt
- // Pointers And Arrays
- //Arrays is a collection of elements of the same data type ,store in contiguous memory locations
- int A[5] = { 2,4,5,8,1 };
- int* ptrArr;
- ptrArr = A;//OR p = &A[0] It will store Address of first element of array
- cout << "Pointers And Arrays"<<endl;
- cout << A<<endl;//Address of A OR A[0] OR (A+0)
- cout << *A << endl;//Value At 0 index :-2
- //When we pass array as function arguments so it will pass as call by reference means so the address of arrays goes not values of array
- //Ex
- //int sumOfArray(int A[]) OR (int* A)
- // Character Arrays And Pointers
- //How To Store Strings
- //size of array>= no of character in string+1
- //Ex Like "John" size>=5 one extra space of null teriminated character '\0'
- cout << "Character Array And pointer"<<endl;
- char c[8];
- c[0] = 'j';
- c[1] = 'o';
- c[2] = 'h';
- c[3] = 'n';
- cout << c<<endl;//without Null Terminated '\0'
- c[4] = '\0';
- cout << c << endl;//with Null Terminated '\0'
- //We can Write Like This
- char ch[20] = "john";
- char chh[] = "john";
- char chhh[] = { 'j','o','h','n' };
- cout << ch << endl;
- cout << chh << endl;
- cout << chh << endl;
- //Arrays and Pointers Are diffreent types that are used in similar manner
- char c1[6] = "hello";
- char* c2;
- c2 = c1;
- cout << c2[1]<<endl;//e
- c2[0] = 'A';// "Aello";
- cout << c2<<endl;
- //c2[i] OR *(c2+i)
- cout << *(c2 + 1)<<endl;
- //c1=c2;(Wrong) , c1=c1+1;(Wrong),c2++;(right)
- // char* st= "hlw";his error occurs because the string literal "hlw" is a constant string,
- //which is stored in read-only memory. When you declare a pointer to this string using the statement:
- const char* st = "hlw";
- // Pointers And Multi-dimensional Array
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement