Thank you so much really, the concept of pointers and arrays was a bit blurry for me, I used to find it very tedious to fathom. Thanks to your videos I can now go ahead and use pointers easily Your channel is really a treasure for anyone interested in C
Another interesting thing with pointer arithmetic is that if you have an array, like `int a[3] = {1, 2, 3};`, and you want to print the second element `a[1] = 2`, you can also write it as `printf("a[1] = %d ", 1[a]);`. This works because under the hood, `a[1]` is equivalent to `*(a + 1)`, and `1[a]` is just a different way of writing the same expression: `*(1 + a)` :)
I remember the first time I saw pointers, it was just mumble jumble and very abstract and hard concept to grasp. Nowadays, it's my bread and butter. This video should have been available when I started! The topic is so simple once you learned it.
I am still struggling to get my head around pointers. Especially the dereferencing part. For eg I declare a pointer Why do i have use * for both referencing and dereferencing a pointer ! and the &
In your example a mem address is a 8 byte long address. So an array is simply a pointer pointing to a place that contains such an address in the consequent 8 bytes. But there is somehow a type involved. A p++ results in an address+size of type, right? But where in mem is this information about of what types a pointer is?
It’s the compiler that maintains how large the value pointed to be the pointer is, and so it’s not really “stored in memory” so much as “in the code the compiler creates”. :-)
@@PortfolioCourses at runtime there is no compiler. yet still "somehow" the cpu knows how many bytes to "skip" when a pointer is updated to point to "the next element" in an array. That information/offset how far to jump/how far to skip must be written somewhere. i wonder how an array of say "ints" (4byte each) is organized in memory physically. somewhere there must be the information stored of WHAT TYPE the array is so the cpu knows how many bytes to skip to reach the "next element".
When we compile a C program that information about how any bytes to skip is put into the assembly (machine code) instructions that the CPU executes. The compiler makes those assembly instructions and it puts the information there, that’s how the CPU gets it.
Nit: arrays are not pointers. Array _expressions_ are converted to pointers under most circumstances (for reasons that are too complicated to get into in a youtube comment). When you pass an array expression as a function argument, what the function actually receives is a pointer. As far as pointer arithmetic goes, the compiler simply generates code to add the right number of bytes to an address to yield the address of the next object; no metadata about types or sizes is actually stored in the executable (except maybe in a debug build).
what if I wanted to print the first value "1" using pointer notation after b++? can *(b-1) get me the value of "1" and in array notation after passing (a+1) to the function, how to get the first value of "1"?can a[-1] get me the value of "1" THANKS A LOT FOR THE VIDEOS :)
Thank you Kevin, very good tutorial. I made nice use of pointer arithmatic in one of my projects after watching many of your videos and learned a lot from you. I used pointer arithmatic to create a wireless packet for one of my projects and expanded on it. Decimal values (for wireless commands and data) are converted into packets for wireless communications (Bluetooth and WiFi) and worked nice. Data packets are created at the transmitter and are easily managed at the receiver side. I made a video about it for hardware designers who do software, like myself. th-cam.com/video/YgizZ6prEFI/w-d-xo.html many thanks for your great videos.
Thank you so much really, the concept of pointers and arrays was a bit blurry for me, I used to find it very tedious to fathom. Thanks to your videos I can now go ahead and use pointers easily
Your channel is really a treasure for anyone interested in C
Another interesting thing with pointer arithmetic is that if you have an array, like `int a[3] = {1, 2, 3};`, and you want to print the second element `a[1] = 2`, you can also write it as `printf("a[1] = %d
", 1[a]);`. This works because under the hood, `a[1]` is equivalent to `*(a + 1)`, and `1[a]` is just a different way of writing the same expression: `*(1 + a)` :)
I remember the first time I saw pointers, it was just mumble jumble and very abstract and hard concept to grasp. Nowadays, it's my bread and butter. This video should have been available when I started! The topic is so simple once you learned it.
It’s true pointers can be so tricky to learn especially as a beginner but once you do learn them they’re not so bad. :-)
that's lesson 101 in embedded mcu programming, thanks for these videos
You’re welcome Christopher! :-)
What a golden video
Thank you for these videos.
You’re welcome Olamide! :-)
I am still struggling to get my head around pointers. Especially the dereferencing part. For eg I declare a pointer
Why do i have use * for both referencing and dereferencing a pointer ! and the &
whatever before or after or anything I do or did or know or knew or anything
This comment is more confusing then pointers
whatever anything I do before or after or anything
It has been awesome.
Excellent I'm glad to hear that! :-)
In your example a mem address is a 8 byte long address. So an array is simply a pointer pointing to a place that contains such an address in the consequent 8 bytes. But there is somehow a type involved. A p++ results in an address+size of type, right? But where in mem is this information about of what types a pointer is?
It’s the compiler that maintains how large the value pointed to be the pointer is, and so it’s not really “stored in memory” so much as “in the code the compiler creates”. :-)
@@PortfolioCourses at runtime there is no compiler. yet still "somehow" the cpu knows how many bytes to "skip" when a pointer is updated to point to "the next element" in an array. That information/offset how far to jump/how far to skip must be written somewhere. i wonder how an array of say "ints" (4byte each) is organized in memory physically. somewhere there must be the information stored of WHAT TYPE the array is so the cpu knows how many bytes to skip to reach the "next element".
When we compile a C program that information about how any bytes to skip is put into the assembly (machine code) instructions that the CPU executes. The compiler makes those assembly instructions and it puts the information there, that’s how the CPU gets it.
Nit: arrays are not pointers. Array _expressions_ are converted to pointers under most circumstances (for reasons that are too complicated to get into in a youtube comment). When you pass an array expression as a function argument, what the function actually receives is a pointer.
As far as pointer arithmetic goes, the compiler simply generates code to add the right number of bytes to an address to yield the address of the next object; no metadata about types or sizes is actually stored in the executable (except maybe in a debug build).
If it is a single dimension we can do like this....can u help me for two dimension sir by giving hint
I have this video on dynamically allocating a 2D array that you might be helpful Monica? :-) th-cam.com/video/ZLc_OpzND2c/w-d-xo.html
what if I wanted to print the first value "1" using pointer notation after b++? can *(b-1) get me the value of "1"
and in array notation after passing (a+1) to the function, how to get the first value of "1"?can a[-1] get me the value of "1"
THANKS A LOT FOR THE VIDEOS :)
You're welcome! Great question, and yes, both of those should work fine. :-) This answer here speaks a bit about this: stackoverflow.com/a/3473686.
@@PortfolioCourses thank you 🙏
@@zizou5638 You're welcome! 🙂
Thank you Kevin, very good tutorial. I made nice use of pointer arithmatic in one of my projects after watching many of your videos and learned a lot from you. I used pointer arithmatic to create a wireless packet for one of my projects and expanded on it. Decimal values (for wireless commands and data) are converted into packets for wireless communications (Bluetooth and WiFi) and worked nice. Data packets are created at the transmitter and are easily managed at the receiver side. I made a video about it for hardware designers who do software, like myself. th-cam.com/video/YgizZ6prEFI/w-d-xo.html many thanks for your great videos.
You're welcome Firas, I enjoyed your video, the step-by-step explanation was very nice! :-)
@@PortfolioCourses Without your videos my video will not be on TH-cam. Many thanks Kevin.
You're very welcome Firas! :-)
whatever anything I do or did or know or knew or did not know I did not finish the video I will finish it inshallah