Pointers in C++ | Pointers Output Based Practice - Download

Pointers in C++,Pointers and Reference,Pointers and Arrays, Pointers and Dynamic Memory Allocation,Shallow and Deep copy, output based practice of pointers,pointers practice,pointers concept,pointers notes
Pointers 
Part 1

1. If the pointer is not initialized but just declared and printed then it will print some dummy value like 0x1

int *px;
 cout<<px<<endl;

output: 0x1

2. If the pointer is not initialized but just declared and the value is found by de-pointing method then it will cause the runtime error with return value other than 0

int *px;
cout<<*px<<endl;

output: runtime error with some error code

3. If pointer is pointed to a value which is just declared but not initialized, then the pointer will still have full address of the memory sector provided to the non-initialized int value;

int x;
int *px;
px = &x;
cout<<px<<endl;

output: 0x6ffe14

4. If the pointer is pointed to a value which is just declared but not initialized and we try to find to value of actual referencing object by the de-pointing method, then the default value of the data-type is printed

int x;
int *px;
px = &x;
cout<<*px<<endl;

output: 0 for int;

5. If the last pointer is not pointing to any reference, them the value of next pointer by the method of de-pointing (de-pointing next pointer not the last) will give an error with the return value other than 0;
int x;
int *px;
cout<<*(px+1)<<endl;

output: runtime error with some error code

6. If the previous pointer is pointing to a data-type (no matter initialized or not initialized data-type) then value of next pointer by the method of de-pointing (de-pointing next pointer not the last) will give some garbage value
int x;
int *px;
px = &x;
cout<<*(px+1)<<endl;

output:3123123 (some garbage value)

7. If an int pointer is pointing towards an int and we print the pointer by giving some index, then it will print the value of that pointer at all the indexes. As we know that it is a single pointer, it will give the exact value at index 0 and at all other indexes, it will give some garbage value.

int a = 10;
int * p = &a;
cout<<p[0];

output: It will print the value of a i.e. 10 but if we use other index rather than 0, it will print the garbage value

8. If in int pointer is pointing towards an int, then we cannot find the value by de-pointing and giving some index as well.

int a = 10;
int * p = &a;
cout<<*p[2];
output: compile-time error

9. If we have a char variable pointing to a char string, if we print the pointer with some index, we will get exactly that single char

char * p = "Jalees";
cout<<p[3];
 output: it will print e

10. If we have any char or int pointer, we can use the index in printing the pointer, but cannot use the index in printing the value by de-pointing it even in the case of an array of int


Part 2
(Pointers and Reference)

1. Reference variable must be initialized as declared because they must have a value and if we just declare them, the value will be null and null is not allowed. In simple words, the reference variable must always keep on pointing towards a variable.

2. If the reference variable is reference to the int variable (no matter if the variable is initialized or not) then the reference variable will be actually containing a constant pointer to that int value. If we change the value of the that int then the value of that reference variable will be changed also. If we print the reference variable, it will always print the actual value of the int number but not the reference.

3. If the reference variable is initialized to the value of other reference or pointer rather than a variable of data-types (int , char) , it will produce an error.

int a = 10;
int * p  = &a;
int &r = p;
cout<<r<<endl;


or  

  
int a = 10;
int &r = &a;
cout<<r<<endl;



output: Error because it must be initialized to a particular variable rather than pointers or references

4. We can change the value of int to which the reference variable is initialized by just changing the value of that reference.
int a = 10;
int &r = a;
r = 22;
cout<<a;

output: the new value of a variable is now 22 which is changed by the reference variable directly


5. We can change the reference variable to point toward another variable even if it is already pointing toward a variable.


6. If we have an int variable which is not a const and we declare the reference variable to be const int * then this pointer only has the ability to see the value of the int variable but not able to change it mean for the pointer it is a const read only variable and changing value by de-pointing doesn’t work.

7. If we have an int variable which is also a const int, then the reference must also be in const int * and the pointer cannot change the value of that variable.

8. If we have a variable of int type and we make new reference variable of int type pointing to that particular int, then if we print the value of reference of that particular int and also print the reference of that reference variable, then both will be the same

int x = 10;
int &r = x;
cout<<&x<<endl;
cout<<&r;


Part 3
(Pointers and Arrays)

1. If we create an integer array, then the array itself will be considered as a pointer and if we print the array the index of the first element of array, which is the pointer to that array will be printed.

const int SIZE = 5;
 int array[SIZE] = {10,20,30,40,50};
cout<<ap<<endl<<array;

output: the memory address of the array’s first index will be printed

2. If we find the value of pointer by de-pointing or pointer address itself (by printing pointer) to any index in the array separately using the reference of array (mean *(array+1) etc.), and also create a new pointer which points directly the same index of the array, then they will point to the same thing (i.e. their address and value will be same)

const int SIZE = 5;
 int array[SIZE] = {10,20,30,40,50};
int p2 = array[1];
cout<<*(array+1)<<endl<<p2;

 output: both will give the same value as both of them points to the same memory location.

3. If we have character type pointer which is pointing to an array of characters, then by printing the value of that pointing by de-pointing will result in the printing of all the characters in the array.

char arr[] = "Jalees";
char * p = arr;
cout<<arr<<endl;

output: All the character of arr will be printed in a single line

4. If we have a character array as above, and we print the value of pointer by de-pointing method, then int rules will be applied and we will get the value of every character separately.

char *p = "Hello";
for (int i = 0; i<10; i++){
cout<<*(p+i)<<endl;
}

output : every character will be printed separately

5. If we have a string and we make it as a char array then the pointer will be pointing to the first character of the string and then printing the value of first pointer to char and printing the next pointers will give the value of the characters.

char *p = "Hello";
for (int i = 0; i<10; i++){
cout<<*(p+i)<<endl;
}
Output: all the characters in the hello will be printed one by one



Part 4
(Pointers and Dynamic Memory Allocation)

1. If we have an int pointer pointing made using the new operator and initiated with another int variable defined then the pointer will not point towards that other int

int x = 100;
int * p = new int(x);

* p = 10;
cout<<x;

output: the int*p will not point towards x rather it will point towards an int variable which is not a saved variable. So, it will print the exact value of x i.e. 100 but changing the value of x doesn’t affect pointer value and vice versa

2. If we have int type pointer that is initialized with some [n] elements,

  •      If we print the pointer, (cout<<p;) it will give the address of the pointer
  •      If we print the value of that pointer (cout<<*p), then the value of int will be printed
  •       But if we use the array index for the pointer and print the pointer (cout<<p[0];), then it will treat it as anonymous int but not as pointer and will give the value of int which is 0 or garbage value if not initialized.
  •      If we use the array index for the pointer and print the value of pointer by de-pointing, then error will be produced because the pointer with array index is treated as int but not as pointer;
  •       If we use the array index for that pointer even more than the index specified in the new int declaration (cout<<q [13213]), then it can run the same if enough memory is available in the same fashion otherwise it will produce a runtime error.
  •       If we have only a single pointer created with new word (int * p = new int) and we use to print the pointer or value by using index then it will give an address or value if asked if enough memory is available in the same fashion.


Part 5
(Shallow and Deep copy)

1. If we have a construct and we are having a member variable int * pointer, the following things are to be considered.

  •      If the member function is initialized with another pointer outside, then the outside pointer and the pointer of the construct will point to the same int variable in the memory.
  •      If we change the value of any one of pure pointer or construct, then the second value will be changed automatically.
  •      In fact, it is a logical error because both will be pointing to the same memory address and changing any one of them will change the other too unconsciously.

                                                                               struct Node {
int * age;
Node (int * n){
age = n;
}
};

int main(){
int a = 10;
int * p = &a;
cout<<"Origional Poniter is: "<<p<<endl;
Node n1 (p);
cout<<"Pointer of Node is: "<<n1.age<<endl;
cout<<"Value of actual before changing is: "<<*p<<endl;
cout<<"Value of Node pointer before changing is: "<<*n1.age<<endl;
//Changing the actual pointer value and checking the effect on the value of both the pointer
*n1.age = 12;
cout<<"Value of actual after changing is: "<<*p<<endl;
cout<<"Value of Node pointer after changing is: "<<*n1.age<<endl;
}

 Output: as mentioned the values will be changing all together rather than creating a copy variable for the construct


2. But in second case, if we have char pointer outside the construct and we pass the variable to construct, it will make a copy of that pointer, contrary to the case of int pointer which keep on making the both pointer pointing to same memory address rather than creating a duplicate for both.

struct Node {
char *name;
Node (char * n){
name =n;
}
};

int main(){
char * p = "Name1";
cout<<"Origional Poniter is: "<<p<<endl;
Node n1 (p);
cout<<"Pointer of Node is: "<<n1.name<<endl;
cout<<"Value of actual before changing is: "<<p<<endl;
cout<<"Value of Node pointer before changing is: "<<n1.name<<endl;
//Changing the actual pointer value and checking the effect on the value of both the pointer
n1.name = "Name2UpdatedByConstruct";
cout<<"Value of actual after changing the pointer of construct is: "<<p<<endl;
cout<<"Value of Node pointer after changing is: "<<n1.name<<endl;

}

Output: No matter we change any of the pointer value, both of them will be behaving independently , mean opposite to the case of int pointer where both point towards the same memory address

4. In a copy construction we must give a reference to a variable of a class or construct but not the actual variable


Download notes in PDF!




Post a Comment

0 Comments