Pointers and 2-D arrays
Вставка
- Опубліковано 9 лют 2025
- See complete series on pointers here:
• Pointers in C/C++
In this lesson, we will see how we can work with 2-D (two dimensional) arrays using pointers. We have explained how 2-D arrays are organized in memory and how pointer arithmetic and dereferencing can be used to work with 2-D arrays.
For more such videos and updates, you may subscribe to our channel.
For practice problems and more, visit: www.mycodeschoo...
Like us on Facebook: / mycodeschool
Follow us on twitter: / mycodeschool
Indians came from another planet, thank you very much for the awesome explanation. Greetings from Italy!
Indians that are educators yes, Indians that are on tiktok no
@@anabeatriz8835 yeah
But in everfield indian always beat other
@@satyamrajput825 Each nation has its excellences but remeber, none is better than the others. Your thought remind me some little man living in Germany some years ago..
@@Phoenix01914 yeah ! i agree with your thoughts but it is not a good thing to condemn anyone's
After about 12 straight hours of scratching my head with pointer exercises, all the answers I needed were in this video. Eternally grateful to you!
Had to watch the video 3 times! :) Hopefully I can explain this correctly. At 4:30 in the video, pay attention. If B were a 1d array, then B[0]=200. Then, using * to dereference gives you '2'. If however, B is a 2d array, then once again B[0] = 200 BUT of a 2d array which means when you dereference with *, you are still at 200 but, now in a 1d array! Confused? Watch it over and over like I did. In other words, to print '2' when B is a 2d array, , use **B. This was fouling me up. Got it now. Thank you.
Didn't understand
Help me
this helped to clear my doubt thanks:)
this makes so much more sense. wow.
I have done a lot of online courses included Strang's (MIT) Linear Algebra, Khan Academy, and Noam Nissan's Nand2Tetris. Not sure of the instructors name, but mycondeschool teaches with the same clarity and intuition of these other great instructors.
Nope, he is from IIIT-Allahabad, not from any IIT. It is only because of his own dedication that he has reached this level of clarity, and not because of his college or anything. Here he is: www.linkedin.com/in/animesh-nayan-0a330812/
*_i really loved this video. This video was so conceptual i have seen lots of C video playlists on youtube but none of the video covered such details. I really admire the effort you made and you are really good programmer :) hope i get to learn java from you_*
hey 9 years have passed look at your comment
I can’t believe you describe such a complex concepts in this way. Good channel, i will see you a lots
Thanks for watching. I mean why?
.
.
.
THANKS FOR MAKING bruh
yes absolutely
That what I thought bro
You steal it
Really enjoy listening to him
You NAILED it! This is the BEST class of Pointers and 2-D arrasys I've ever seen. FOR GOOD. Thank you so much!!
This is the best explanation that I have seen so far for this topic. You are really amazing !!
This was the best tutorial video on pointers for multi-dimentional array that I have ever watched. Thank you so much for making such tutorial videos,it helps us a lot. Thankyou very much
omg I love you sir 😂❤
I've been looking for this explaination for literally months and never understood how to handle 2D arrays with pointers untill I saw this video
thank you so much for making this video ❤❤
good job!!!! you know i can't understand C pointer and some other things about data structures and algorithms ,before watching these videos ! you are the best ! thank you so much ! ---you fans from China
Explained really beautifully! Thanks for allocating your time for creating this series of lectures.
It helped me to think about these examples by using the word adress. Also, that part in the begining of the video it is shown that in 1D array B[1] == *(B+1) and
&B[1] == B+1. It helped me to think about multi dimensional arrays as things, where the previous two rules apply. Also, keep in mind that if you have a 2D array like in this video B[2][3], if you write B[0] that is a 1D array (1D array which has 3 elements in it). Which means that B[0] is a memory adress. These three insights (or four depending on how you count them) were what made me understand this. Hope this helps to someone struggling to understand this. If you don't get it immediately, watch the video again, think about it and you will understand it.
Usually hate scouring for coding videos on youtube cause they all suck, but this video really helped! Came into this with a lot of scepticism, but was shocked with how he dumbed everything down to make it understandable (unlike my prof). Thanks for making this video bro, cleared up alot of questions.
it's the best tutorial that i've heard
thanks for all effort you made !
The B[0] = &B[0][0] part in this video is so good that it helped me click something I didn't understand all semester. Thanks for the video!
Exactly! B[0] is itself an array and when you just put the name of an array, that's the memory adress of the first element of the array. I think I got this, and if I did, then it clicked to me for the same reason that it clicked for you.
B[0] = &B[0] = &B[0][0] = B = &B = *B all gives same thing i,e same address
@@happyteen7195 Yes, B[0] gives you the first element of the outer array, which is itself an array, so you get the address of that array. So B=B[0]
But then the address of the array B[0] is the address of the first element of the array, which means
B = B[0] = &B[0][0].
Then, we also know that *B is the same thing as B[0], so
B = B* = B[0] = &B[0][0]
Then we know that &B = B because the address of the array = array.
&B = B = *B = B[0] = &B[0][0].
I don't know why he didn't write code as an example. I will give you the actual code in the end of the comment.
The other way to look at it is this, if we write &B, we get a 3D array whose first element is B, so if we print the that 3D array we will get the address of its first element which is B, and address of B is the address of its first element - B[0], and B[0] is also an array, so the address of B[0] is the address of B[0][0]. Basically, we are talking about the address of B[0][0] all the time.
#include
int main(){
int B[2][3] = {{0,1,2}, {3,4,5}};
printf(" B = %lld
", B);
printf(" &B[0] = %lld
", &B[0]);
printf(" B[0] = %lld
", B[0]);
printf(" &B = %lld
", &B);
printf("
");
int A[4] = {1, 2, 3, 4};
printf("A = %lld
", A);
printf("&A = %lld
", &A);
return 0;
}
@@happyteen7195 Here is a video which explains this with code ua-cam.com/video/falKBQVWSLw/v-deo.html
@@happyteen7195 Another important thing to say, and I am saying this while hoping that this will not just confuse you more, is that if you write
int (*b)[3] = B;
computer will treat b and B differently. In what sense? In a sense that if you write &b, computer will treat that as a double pointer, as an address of b. It will treat b as int** [3].
On the other hand, if you write &B, computer will treat &B as a pointer to a 3D array whose first element is B. Computer will treat &B as int* [2][3].
In other words B is equal to &B, but
&b is not equal to b.
Here is a program for you to check out on your own, I wanted these warning signs intentionally btw so that compiler tells us what types B, &B, b, &b etc. are
#include
int main(){
int B[2][3] = {{0,1,2}, {3,4,5}};
printf(" B = %lld
", B);
printf(" &B[0] = %lld
", &B[0]);
printf(" B[0] = %lld
", B[0]);
printf(" &B = %lld
", &B);
printf("
");
int A[4] = {1, 2, 3, 4};
printf("A = %lld
", A);
printf("&A = %lld
", &A);
printf("
");
int* a = A;
printf("a = %lld
", a);
printf("&a = %lld
", &a);
printf("
");
int (*b)[3] = B;
printf("b = %lld
", b);
printf("&b = %lld
", &b);
return 0;
}
To all those who have a doubt that at why 8:29, (B+1) and *(B+1) both are equal to 412, here is the answer.
B+1 gives the address of a pointer which is pointing to a 1-D array of size 3. The type of the pointer is such that it points to a 1-D array and in this particular case it is pointing to the 2nd element of the 2-D array, that is, row B[1]. So when you do pointer arithmetic, it will increment or decrement with the size of a 1-D array of size 3. For example, B+2 will give the address of a pointer of the next row in the 2-D matrix, that is, address of B[2].
On the other hand, *(B+1) is a type of an integer pointer which points to the first element of the 1-D array. This is an integer pointer and arithmetic on this would result in increment and decrement with the size of an integer, that is, 4 bytes. Hence *(B+1) +1 would give the address of the next integer!
thanks
Thanks man ! that was helpful
So it just so happens that the address of the pointer to second subarray (B+1) is the SAME as the address of the first element of second subarray *(B+1), am I correct?
@Mangosrllysuck That's what bugs me as well about his comment. Here's how I understood this video. Let's see what *(B+1) + 1 is.
*(B+1) is the same as B[1]. So, we get B[1] + 1.
We know that B[1] is itself a 1D array.
B[1] is therefore the memory adress of that 1D array (elements of the array that I am talking about are B[1][0], B[1][1], B[1][2]).
So, B[1] + 1 is the same thing as &(B[1][1]).
It is the memory adress of B[1][1]. In other words, it is the number 416.
@@Mangosrllysuck Did I get it right, is *(B + 1) + 1 equal to 416?
i can not find someone bettr than you in anything you explain ... you are awesome ...keep up the good work ..thanks
Dude, u rock! U explain stuff much better than IIT profs. And yeah, this is coming from an IIT fresher.
kaunsi IIT Hai?
Please watch the video "Pointers and arrays" in this series. It will help you understand the concept.
Please man come on and do more videos on data structures and interview questions , that make our lives good
You should really consider coming back to this channel brother. We need you!
A best teacher, explains clearly in one single line,
superb video series , none I have seen that clears concepts in pointers this deep and lucidly.
Every other videos where great, except this one.. i had to watch it 4-5 times (the part which starts explains 2d array) over to get the pont. anyway, i'm very much thankful that it helped me understand somewhat.
One of the best stuff available on the internet. :) Keep it up.
Holy cow, this is another realm of C programming and can write pointer codes my ways. I would never be able to figure out how all of the advanced pointer code works without understanding the fundamentals.
u r simply the BEST on youtube.!!
sir really you have very deep knowlege.My concepts are cleared so much ,now i have to just apply it in real code
This video was infinitely helpful, thanks for posting!
Thanks Adamou :)
Thank you for this tutorial. Very easy to understand!
Pointers are no longer over my head. Thanks a lot !
Great explanation... No need to study books. Thumb up..!!!!!!!!
this tutorial is quite amazing. Thank you and please keep it up
You are an Angel! Thank you so much for all these amazing videos!
bro your videos are so good and concise
thanks for making this video, I was very struggling with pointer to array. You have explained very well
Thanks a lot, the most informative and to-the-point video about this subject
&B is address of 2-D array , B is address of first 1-D array in the 2-D array , *B or B[0] is address of first integer in first 1-D array. All of these start at same address. &B[0][0] means address of B[0][0].. once again its the same.. the objects are different in size but their starting address is same. Feel free to ping back if still confused.
Sir *B means dereferencing then how it could be &B[0][0]
how B[0][0] and &B[0][0] the same the 1st give me the value and 2nd give me the address
@@buradaleelaprasanna5131 *B or B[0] is address of first integer in first 1-D array. -> it is right ( you can watch previous video about it)
&B[0][0] you can split it -> ( & and B[0][0] ) means B[0][0] is the first element of 1-D array so after add " &" , we have adress of the first element of 1-D array
This is the only lesson I found difficult that this man taught
Excellent walk-through. Well explained with very good examples. Thank you so much.
Hi Raj ,
We have a lot of requests for videos on these areas and we definitely want to cover them soon. We currently have only one mentor creating videos and things are slow. We are trying to speed up by pulling in more contribution. Sorry to keep you waiting , but these videos are promised and are high in our priority.
hello :)
Very helpful.
A very eloquent way to present the topic. Hopefully many people will benefit from this. I certainly did for which I'm thankful.
I'd appreciate if there was also an explanation as to why a pointer to an array of pointers has to be declared as int (*p)[3] although it may be a more advanced topic. Why can't int **p work? It is seen on the picture but not explained in full detail, i.e. the multidimensional array is located continuously in memory. So because of that in pointer arithmetic advancing the pointer with 1 must not move it by one byte (as if it were int **p) but by three (because of int (*p)[3]). int (*p)[3] literally means "a pointer to an array of three integers". int p[3] means that p is an array of 3 integers. Adding the asterisk makes p a pointer to an array of 3 integers, which is equivalent to the definition that the name of the array is a pointer to its first element.
+simich you have to know how to handle an expression .This links will help you c-faq.com/decl/spiral.anderson.html . This way you can see how the compiler looks and interprets an expression. (*p)[3] it states that p is a pointer to an array with 3 elements.
It is because of array decaying. Whenever you assign a x-dimensional static declared array to a pointer,one dimension is lost(in the example (*p)[3]=B is really like (*p)[3]=&B[0],and &B[0] is an address to an array of 3 elements). You could ask,of,so I could do *p =&B[0] cuz I know I can do p=a where a is 1d array. But the compiler won't let you do this.
At compile time it that B[0] has 3 elements ,and wants you to pass a pointer to 3 elements.
What I'm saying about array decay,you see that (*p)[3] know there are 3 elements on a row,but doesn't know how many rows there are.
You should really read a lot of topics on these ,it's not easy and it's tricky.
All I say is this. Arrays are not pointers and pointers are not arrays.Even if you can do *pointer and *array,and you can pass an array to a pointer,deep down they aren't the same.At compile time you know the sizeof static declared array,but you can't know the size of what the pointer points to just until run-time,so it has some loss when passing as parameter.
Sir.. you doing a great job.. thank you so much..!!
So good explanation __
you are really a great teacher! such lucid explanation.
This is cool 🌊 . I still haven't grasped the concept the way 2D array name makes partition to the memory but if that foundation is pre-set by law(law of C language 😝) Then you have my gratitude brother. I got this.❤️
very helpful video sir, the way you have explained is really appreciating, thank you 👍👍
What he says is totally right , it is in fact a little different than a 1D array. Just try it in your compiler and you'll see !
@mycodeschool Thanks to you pointers is clear to me. Thank you very much
the best video ever!!
A very good explanation on pointers. Plz give answer for *(B+1)[0]
Thank you so much you solved my problem !!
int *pB=B; for a 2 by 3 array and int (*pB)[3]=B; both compiled perfectly returning same output without any error using Codeblocks.
First off, thank you so much for these videos. They are phenomenal.
I'm at 6:10 and am a bit confused about *B as it's not declared as a pointer. How is *B = B[0] = &B[0][0], I would guess that *B was dereferencing, but then wouldn't get the value instead?
B is the name of our 2d array.
That name acts as a pointer to the first element in the array.
But the first element in the array is not an int - it's a 1d array of ints, called B[0]
Hence, B = &B[0]
Dereferencing gives us *B = B[0]
Now, B[0] is a 1d array of ints.
We know that the name of an array can be used as a pointer to the first element in that array.
So the name B[0] points to the first element in that array of ints
In other words, B[0] = &B[0][0]
Hope that helps.
PS. Also worth noting that since *B = B[0] = &B[0][0]
if we dereference everything we get
**BB = * B[0] = B[0][0]
In other words, dereferencing the name of a 2-d array twice gives us the first element in the first row.
@@samdavepollard thank you!!!!! I was so fucking confused!!!
*B gets you B[0], however B[0] is itself an array and array, so B[0] is a memory address of the array whose elements are B[0][1][2]
Great exploration
For n dimensional array by dereferencing it by n asterisk, it will completely dereference the pointer to give the value
for e.g. b[2][2][2],
***b = value in the b[0][0][0],
so ***b + 1, here 1 is added to the value of array
**b + 1, means that pointer to int array ( 4 byte is added )
*b +1, means that pointer to 1d array of size 2( 8 byte is added)
b + 1, means that pointer to 2d array of size 2X2 ( 16 byte is added)
b+1 gives 412 !! *(b+1) should gives the value stored in that particuler address why it gives an addresss ??
That is what i was thinking too.
@Vaneet Gupta what is *B
@@charansashidhar2214
*B returns the initial address of the first element in the first row of the multi dim array. In this case, B and *B are the same, since both represent the address of the initial element of the whole multi dim array which is nothing but the first element in the first row.
When we dig into the multi dim array,
say, B[0] returns the address of the first element of the initial row, *B[0] returns the value at the first element of the initial row.
B[1] returns the address of the first element of the second row, *B[1] returns the value at the first element of the second row.
When we still dig deeper we get the values,
say, B[0][0] returns the value of the first element in the first row.
Don't get confused about the following,
internally the array works in the way when we get B[1][1] ,
*(*B[1] + 1) //this is the structure of the element B[1][1]
=> *(&B[1][1]) // returns the address of B[1][1] which is then dereferenced to get the value.
=> B[1][1] // C provides an easy way to access the elements directly from this step by ignoring the above steps
=> value at B[1][1]
This is actually explained in some previous video , when they talk about generic pointer and why the pointers have types
like int *p1;char *p2 ......
Because we can dereference them later and for that we need to know what exactly lies at address 412, is it an int ? (no) there is an array B[1] stored at 412 .
Now 412 is actually address of an array B[1] (let B[1]=A)
so the statement printf( *(B+1) )=printf( B[1] )=Printf(A) which gives base address/address of first value ( A[0] or B[1][0])
hope it helps :)
@@saimanognanelavelli8005 do u wanna say B+1 AND *(B+1) are the same?
Thanks man! I had to listen to this couple of times, but it made sense.
Thanks for your great efforts 🔥🔥😍🤗
thank for your helpful lesson
great explanation
great explanation..........!
Super super helpful and clear! Thank you so much!
You have explained well
Great effort, great explanation. Thanks.
Very well explained!
holy shit what a golden video! THANKS
Very much helpful. Thanks team 😊
very good lecture....specially memory allocation topic is very nicely explain
Very good video. Thanku soo much. Helped a lot.😊😊
Nice explanation!
How come deferencing a pointer still giving as the address and instead of value at that address-> timestamp[ 8:28 ] => here *( B+1) = B[1] how this can equal to &B[1][0] = 412 ?......isn't deferencing of pointer var should give us the value and not the address ???
You're a Monster.
At 8:27 , you were trying to explain B+1 and *(B+1)....Its a bit confusing ! How B+1 and *(B+1) giving the same result , i.e. 412 ???? Please explain :D Thanks !
+Yash Pandey That is an error I suppose. It should give us value 4
9:22 reference with green colour. NICE
Does anyone here understand why (b+1) and *(b+1) both produce 412? If you dereference shouldn't it produce the contents of 412 which is 4?
Exactly my doubt
Abhimanyu Bhardwaj Yea, I still can't understand. Hopefully someone will help us.
did u get the answer to your doubt? i think its because :
Thinking that 2-D array is an array of arrays,* (B+1) or B[1] should get you to second array (Second Row) which is an array instead of int element. And when array is assigned to something its the address of first element that gets assigned. So de-referencing it only once gets you the starting address of second array. De-referencing it again should get you the 4 ie **(B+1)=4. hope that makes sense
That makes so much sense - thank you!
+Shishupal is **(B+1) and *(*B+1) the same or u made a mistake ?
Very Valuable Video :)
An excellent video mate! I dont know what your profession is But you should definitely teach in universities!! Thanks a lot from behalf of all the programmers you helped! (including me)
He is software engineer at google
@mycodeschool shouldn't * print the value instead of address? Please clarify
in 2D array to get the value of that particular address you have to write *(*c+i) for example coz * is just a pointer to an integer
See try to see it this way there is an array inside an array so the first time it is only going to give the address of the array and then we are going for the address of the variable name of the one dimensional array.
But the value of B[0] is an array, and arrays are represented as a memory adress of their first element.
awesome content
Awesome stuff!
finally got it. Thank you!!!!!!
Thanks for Making 😊
Thank you very much, my friend!
sir, I will understand good from your excellent explaination thank you so much
Thank you! It helps a lot!
But in the previous videos you said that arithmetic operations on array will result in error but such incrementation operations can be performed on pointers. But here B is the name of the array.
thank you
thank you for these tutorials!
Thank you U R THE BEST
thank you very much 😍♥
great tutorials thank u
thank you. the vedio was very helpful
At 11:23 b is the address of 1st element that is 400 and *b is 2 and *b+1 is 3 and *(*b+1) is the address of 3 that is 404 not 3
Helpfull, thank you .
Oh wow! This just got complex :D Woooohooo!!
The precedence of the signs does not affect the operation that is doing *(b + 1) + 2. According to my concepts, by the way you are locating the data, it goes to the second element of the array, then desreference and adds 2 to the value.
Therefore I think it would be necessary to use the parentheses *((b + 1) + 2).
Could you please correct me, thank you very much for taking the time to explain.
Thanks