mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 13:06:29 +00:00
Update Pointers_C++.md
This commit is contained in:
parent
dc2f71f93e
commit
fb2c245e42
@ -56,14 +56,14 @@ Now, suppose we have a mechanism that allows us to **manipulate the variable via
|
|||||||
|
|
||||||
Let's see an easy to understand the advantage. Suppose, we want to pass a large variable to a function.
|
Let's see an easy to understand the advantage. Suppose, we want to pass a large variable to a function.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Passing via usual way(by value) will create a copy of the large variable.
|
Passing via usual way(by value) will create a copy of the large variable.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
But now we will pass the address of a variable and use the mechanism to avoid a copy of the large variable.
|
But now we will pass the address of a variable and use the mechanism to avoid a copy of the large variable.
|
||||||

|

|
||||||
|
|
||||||
So basically, we are passing values indirectly using just the address of a variable.
|
So basically, we are passing values indirectly using just the address of a variable.
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ The best practice is to declare pointers in different lines.
|
|||||||
|
|
||||||
As we know, pointer variables store the address of another variable. Therefore, **the value must be an address of a predeclared variable of the same datatype as of pointer variable**.
|
As we know, pointer variables store the address of another variable. Therefore, **the value must be an address of a predeclared variable of the same datatype as of pointer variable**.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -326,7 +326,7 @@ What did you observe?
|
|||||||
|
|
||||||
You can see that arrays are also using pointer, which points to the first element of the array.
|
You can see that arrays are also using pointer, which points to the first element of the array.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
But a fixed array is not exactly a pointer, let's see:
|
But a fixed array is not exactly a pointer, let's see:
|
||||||
|
|
||||||
@ -361,7 +361,7 @@ Yes, add `sizeof(data type of array)` bytes to the address of the first element
|
|||||||
|
|
||||||
In C++, we can use basic math operations(addition and subtraction) on pointer as well. But it is different. If you add(or subtract) integer $a$ to pointer containing some address, then it will add(or subtract) `a*sizeof(data type of the pointer)`(ex. `a*(4 byte)` for integer pointer) to its address.
|
In C++, we can use basic math operations(addition and subtraction) on pointer as well. But it is different. If you add(or subtract) integer $a$ to pointer containing some address, then it will add(or subtract) `a*sizeof(data type of the pointer)`(ex. `a*(4 byte)` for integer pointer) to its address.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
As array elements are continuous(sequential) in memory, **pointer arithmetic is basically used when we access array elements using [] operator(ex. array[3]).**
|
As array elements are continuous(sequential) in memory, **pointer arithmetic is basically used when we access array elements using [] operator(ex. array[3]).**
|
||||||
|
|
||||||
@ -478,7 +478,7 @@ Now, let's check how well you have understood things so far.
|
|||||||
|
|
||||||
Pointer to Pointer is a pointer that holds the address of another pointer variable. It can be declared by putting two asterisks (`**`) instead of one.
|
Pointer to Pointer is a pointer that holds the address of another pointer variable. It can be declared by putting two asterisks (`**`) instead of one.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -534,7 +534,7 @@ Observe the below image and try to understand the shown pointer arithmetic.
|
|||||||
|
|
||||||
Note that, `a[x][y]` in pointer arithmetic sense is basically `*(*(a+x)+y)`.
|
Note that, `a[x][y]` in pointer arithmetic sense is basically `*(*(a+x)+y)`.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Here `sizeof(a[0])` or `sizeof(*a)` is 3 integers, i.e. 12 bytes and therefore in pointer arithmetic, if we add 1 to `a`, then it basically adds 12 bytes.
|
Here `sizeof(a[0])` or `sizeof(*a)` is 3 integers, i.e. 12 bytes and therefore in pointer arithmetic, if we add 1 to `a`, then it basically adds 12 bytes.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user