The Complete Guide to Pointers in C++ for Programmers | Concepts Related Pointers
Ahmad Raza
Software Engineer | CGPA 3.83/4.00 | Global Nominee @NASA?? | 15X Inte'l Hackathons & Hackathon Finalist | LeetCode 350+ | 2X Hackathon Mentor | IELTS & AI Instructor | MERN Stack & Gen AI developer | C++ Python
So, everyone when starts learning pointers, he/she gets scared by this name. What are pointers? I listened about this from the seniors, it's very difficult and complex to understand how the poniters work. So, all these questionts are okkay but not now, in this article I'm going to remove and clear your doubts, your anxieties and to make your concepts strong.
What are Pointers ?
So, basically pointers are the variables that can store addresses of other variables or set of variables like arrays. Further pointers can be used to create Data Structures dynamically or to create memory dynamically on heap. Pointers are also used to iterate or traverse through elements like in arrays or lists. But the main question is how ?
So, initially when we create an array statically Like int arr = [ 20 ], so firstly arr doen't has any value, it has address of element at the first index, and when we want to get values of elements at nest indeices, we basically increment the address. For example if the address of first element is 0x1000 and the array is of integer type. And we want to move to index number two then for this we just write arr[ 2 ], but in the backend the calculation is like this :
baseAddress of arr + ( indexNumber * number of bytes of Data type )
which can be in case of index 2, as 0x1000 + ( 2 * 4 ) equals to 0x1008. I write 4 because integer data type consumes 4 bytes. So, the array will move to address 0x1008. And now we can get the value present at this address, we can add value at this address, what ever we want to.
So, the demonstration of this array traversal can be achieve in many different ways:
array[ i ] , i++
array, array++
( array + i ), i++. Don't worry about * used, w'll discuss it next.
We also use pointers to pass variable by reference. Let's take an example:
suppose we have a varibale : int number = 100;
Now we create a pointer variable to store address of 'number' : int *ptr = &number. (As pointers can only store address. So, by using '&' sign we basically storing address of number variable in ptr ).
Now if we do like : ' *ptr = 200; ' ( Here, we used ' * ' sign, because currently ptr has an address of number variable, so by using '*' we are actually dereferencing ptr variable so that it can now store value )
And, as we are changing ptr not number, but the value in number will also be changed because ptr has the same address as that of variabl 'number'. So, we changed a value in a variable without using it.
领英推荐
How To Use Pointers ?
int number = 100 ;
int * pointer = &number ;
// Suppose address of number is "0x77fs23e5"
// So, pointer will store address "0x77fs23e5". Although it has its own address
// And *pointer will have the value 100
// We can also change the valu of number by using pointer variable
* pointer = 500 ;
// Here, we are using * to store value as it can only store addresses
Pointers in 2D array ?
In 2D arrays pointer become slightly advance. Consider pointer notation for the two-dimensional numeric arrays. consider the following declaration.
// indexes = 0 , 1
int nums[2][3] = { { 16, 18, 20 }, { 25, 26, 27 } };
//Let's suppose base address of nums is 0x1200
Now if wants to point to second element of array which is also an array { 25, 26, 27 }. Then we know that what to do, the farmula is :
base Address + ( indexNumber * bytes in Data type ).
But as we know here we have arrays within array. So, here our formate will be somehow different. Like if we want to get the value of 1st element of 1st array, nums[ 0 ][ 0 ], then what we have to do is ( ( nums + 0 ) + 0). So first of all here (nums + 0) will point to the first element of nums, which is again an array, so to get thats nested array's first element we again have to move to 0 element of that array so, * ( *(nums + 0) + 0) will give us our nums's first array's first element with is present on nums [ 0 ][ 0 ].
So, inner deferencing will help us in moving outer array and outer deferencing will help us in moving inner array.
Dynamic Memory Creation Through Pointers ?
So, when we does'nt know in advance that how much memory cells, user wants to create or we can say that how many element user wants to add in our pre defined array, then at that situation we can have three choices.
//If we want to create 1D array
int size = 0;
// ask from the user the exact amount of cells he needs to create memory
cin >> size;
// And create that memory on heap, dynamically
int * array = new int[size]
If you want to create 2D array then;
int rows = 0, cols ;
cin >> rows, cols ;
int ** array = new int * [ rows ];
// here the first * represent that this is an array having address, and second * represents that in each element there is again an array having address of first element
// And here in int * [ row ], represnets that each element in row will have addresses of elements having int Data Type.
for ( int i=0 ; i<rows ; i++) {
array[ i ] = new int[ cols ];
}
So, pointers are very easy to understande, just read documentations and make your mind to focus eevn for small time, you will surely crack it. Thanks !
Section Leader @Stanford Code In Place | Project Admin GSSOC Ext | CS50 Puzzle Day Winner @Harvard | Hackathon Winner @lablab.ai | 4X Hackathon Mentor | 2X Hackathon judge | 20X Hackathon Participant | Game &Web& AI Dev
6 个月good write up. keep it up Ahmad Raza
Aspiring Computer Engineer | Core team member - GDGOC-UITU | Creative Graphic & Web Designer | Data Science Enthusiast
6 个月Keep learning!?
Computer Science ? CGPA 3.98/4.0 ? Meta Hacker Cup ? Leetcode_Python ? Laravel ? C/C++ ? Int'l Hackathons @lablab.ai ? @ICodeGuru ? Science Teacher High School
6 个月great work cout<<array_name; // will print the address of array right?:)
React Native Developer | MERN | BSSE | Int'l Hackathons | LeetCode
6 个月Good bro