Hey you are seriously gifted man... I honestly wish I was paying you my tuition and not my school. Like the other comment said you are putting my professors to shame. Keep what you are doing man... you are great at it.
Man, you're an amazing teacher. I was fed up at college when i wasn't able to understand anything about pointers and your videos helped me immensely to get those stuff. Keep up the good work man. Hats off to you!
Small add up to the course: if you are trying to compile these codes from a linux terminal, you might get an error saying " expects argument type int ". its because ,when you try to get the memory address by printf, you cannot specify it as %d as its shown in the video. instead of that try to put %p. example: printf("%d ", *p); // this might works fine since the out put is an integer as its expected. but to get the address. printf("%p ", p); //this works for the address
Oh My God! After watching this video my thought on pointers concept is totally changed as an easy concept. Perfect Explanation which I never heard before !!!!! :-) Thanks a lot
when you were explaining , you used a statement like "ADDRESS OF P".....But when we are using a pointer P, we are actually referring to the address of integer variable A rather than the address of integer to pointer P...To refer to the address of P we must use another pointer to integer (Cuz P is also a pointer of type integer or a pointer to integer) and we will store the address of pointer variable P in this variable..... :D By the way , your videos are really brilliant , the best i have seen so far ! Thanks :)
The way you spent tym in making these videos as well as those captions(subtitles), is just great. Thank you so much for spending time and making our future bright.
Thank you so much! Being a non-computer science major, I've been studying programming myself and the concept of pointer has been one of the toughest to grasp. Now it makes more sense :)
man you're very clear about the explanation which my prof couldn't do over the course of 2 classes. Thanks a bunch! Saved me from understanding the material.
I can't believe I can spend so long confused by pointers and I come across these videos and it all clicks together. Thank you! (Also wonderful accent, it's like the diet version :))
You're a legend bro! I hated dealing with pointers before coming across your playlist, now they seem sensible. Thank you so much! You have brought me joy, and I look forward to making it to your data structures playlist. Best wishes, and I'm excited to see what comes next with this series on pointers.
Im in my first semester of grad school and taking an intro to C programming class. The professor ruined this concept by not explaining it well and glossing over it. I’ve been struggling with these for 4 weeks and I’m so glad I found these explanations. Everything is coming together in my brain
Sir, first of all - hats off You’re doing an amazing job. There is one correction however, sizeof is not a function, it’s an operator which is executed at compile time.
I do not get your question. Garbage collection is freeing dynamically allocated memory (allocated using malloc or new operator) that is not being used at any point. Whats the relation here?
Thank you for the videos. Your explanations make pointers much easier to understand. I have a question, though. Why use pointers to manipulate the int value. Wouldn't it be easier to just change the value directly. I guess I thought pointers were used to copy the integer value so it could be manipulated without changing the original variable value.
Wow. I did not know you can initialize the "*p" to the address. My book always did the long way. Now I understand how you can pass addresses through the parameter of other functions. Wow thank you for clearing that up. I was getting confused because I thought "*p" only contained a number at which "p" had the address of. But now I see that *p is to tell the c system the the variable p is a pointer!! And omg it makes so much sense
I did a research on this topic. What I found is that the narrator of the video Animesh is alive and well but the co-founder of mycodeschool who went by the nickname HumbleFool passed away somewhere in 2014-2015 in a car accident. Harsha (HumbleFool) was India's one of the best competitive programmer. Animesh (an alumni of the Indian Institute of Technology) co founded the mycodeschool with Harsha somewhere in 2009. I also read in an article that Animesh is currently working in Google. However I cannot understand why Animesh has stopped uploading videos, maybe it is because of the sudden demise of his founding partner. (I googled all this so nothing is official and I could be wrong)
Hi, thanks for the great video. In the example at 6:09, I think the printf statement should read ("Address of a is %d ", p) instead of ("Address of p is %d ", p). p is pointing to a, and by printing p we're showing the address of a, not the address of p.
At the 6:24 point of this video, the 1st printf statement("address of p is %d",p) which turned out to be 3144576 there should probably be ("address of a is %d,p) ,shouldn't it be? Similarly,the 2nd printf("Value at p is %d,*p) printing 10 should (as far i could understand) ("Value at the address stored in p is %d",*p). Kindly,clarify. Thanks.
You are correct! At 6:24 could also be: printf("value of p is %d" ,p) I think our teacher got a little confused himself. Anyway I think he has the best pointer lessons on RUclips
dont be confuse. see p=&a learn to read it like this. left to right. address of a is assign in pointer variable p. *p will print value of a and p will print address. remember "HOW TO READ". address of a is assign in p. hope u understand. lol i saw now u posted 2 years back lol any how
Hey I have a doubt what happens if I print a pointer with a prefix operator I mean like printf(“%d”,++(p)); when I actually run this code I am getting output with actual address minus 4
I think that instead of "Address p is" you could have gone with "Address stored in pointer variable p" or "Address pointed to by p" so that beginners could avoid confusion with "Address of p". (Take it from me as someone who for a long time could not understand pointers because of how people and books taught the topic.) Otherwise, great video.
I am using GCC compiler by GNU and here's the output I get after executing the code: The address of a is 0x7ffee3ebfa0c The value at address p is 10 The size of the integer is 4 The address of value next to a is 0x7ffee3ebfa10 The value at address p+1 is -471074272 Also, to find the value at address p+1, you have to enclose the variable within brackets as (p+1) before the pointer operator, else the compiler increments the value by one unit, i.e., it returns 11.
Dude, i love your videos and i want to tell u i hope u get the same help u r proving us in ur advanced career topics by other cool guys like yourself:))
hello, Admin. Thank you very much. *(p+1) will give us some garbage value. but *p +1 will increment the value that p points to 1. However, my question is will the contents that p is pointing to shall change?
In 6:21 Did you mean to write in the printf statements that the address of a is the value of p? because If I understand it correctly, what you showed was the stored value in p, which is the address of a, when you wrote that the address of p is the value in the pointer p.
*My takeaways:* *& vs *:* int a=10; \\ declare a variable int *p; \\ create a pointer, and we have to initialise it otherwise we will have an error p = &a; \\ initialise the pointer 'p' with the memory address of 'a' (i.e. &a) int *p = &a; \\ we can also create a pointer and initialise it in one line of code int* p = &a; \\ this does the same as the above code *Be aware when we are not creating a pointer, 'p' is the pointer (i.e. the memory address) and '*p' is the value at the memory address pointed by 'p'.* For example: '*p' is the value at the memory address of 'a' (i.e. '&a') =the value of 'a' =10 *p = 12; \\ this is called dereferencing, we can change the value of 'a' to 12 using the pointer *Index memory* p is the memory address of 'a' p+1 is the memory address of 'a' + the size of 'int' (i.e. 4 bytes), and if 'a' is in other data types, the size is different
This is one dangerous thing about C, using pointers you can access a memory location that you are not supposed to access. As long as you are reading whats written there, its fine, but if you trying to write something... you may be over-writing something critical. So program will either crash or give some unexpected behavior. Write to a memory location which is assigned for some variable, not anywhere.
Hlo Sir... Like ur way of teaching very much...Superb job...u r doing over here... After listening ur 1 or 2 lectures i m nw bit comfortable with pointers. :) I hve a small query " U r repeatedly using %d to print the address. But when i use it in my C program it is giving me a -ve number. But if i use %u then it gives me the right address that the pointer points to" Why the two values are different.?
*p should print the value and not the address .. When we are printing p, we are trying to print the address... Your compiler is not allowing that because its expecting an int for "%d" ,,, try printf("%p ", p).. "%p" means you are trying to print an address.
Dear mycodeschool and listeners! could you explain me more about your code "printf("Address of P is %d ", p);" (at 6:22) in time. as my understanding, this command will display the address of available (a) that is pointed by pointer P; I think if we want to display the address of P we should use printf("Address of P is %d , &p"); very happy and thank so much for your lectures!
Nice video but there is a serious error at 8:28 where you say that because the size of an integer is 4, then p+1 is 4 bytes further on from p. It's correct that it's 4 bytes further on in this case, but not because it's the size of an integer (which for the compiler you are using just happens to be 4). It's the number of bytes needed to store a memory address, which in this case also happens to be 4. If you change the data type to char, which has a size of 1 byte and the pointer + 1 would still be 4 bytes on. Also at 6:21 where you have "printf(address of P")". It's not actually the address of p that you are printing but the address stored at p, which is the address of a. Also, if you want to print memory addresses the proper way to do it is to use %p as the format string and (void*) &p as the argument. This last point is probably beyond the scope of the video but you run the risk of getting errors if you treat a memory address as an integer. .
Every byte in memory has an address. So, if you have an int that's 4 bytes and its address is 1000, 1001 is actually the 2nd byte of that int and 1002 is the third byte and 1003 is the fourth. Since the size of an int might vary from compiler to compiler, it is imperative that when you increment your pointer you don't get the address of some middle point in the int. So, the job of figuring out how many bytes to skip, based on your data type, is handled for you and you can just use whatever value you get and not worry about it.So as you were saying for a character the (p+1) will refer to next byte as char type only takes on byte.So in a nutshell pointer arithmetic doesn't work in bytes but in terms of sizeof(pointer base type)-bytes sized blocks.
I agree with what you are saying in the statement above. What I am saying is that it is ambiguous in the video when you use 4 byte addresses and a datatype that uses 4 bytes in the same example. It becomes unclear whether the the fact that the pointer plus 1 is 4 bytes on is because of the size of the 4 byte integer datatype or because you have a machine that uses 4 bytes for addresses. The reason why p+1 is 4 bytes further on is because in the video you are using 4 byte addresses. For example if you had a 64 bit machine and a 4 byte integer datatype then addresses i.e. pointers would occupy 8 bytes and each item of data would occupy 4 bytes. So the address at 1000 to 1007 would point to the 4 byte integer a. Incrementing the pointer to p + 1 would be to use the 8 byte address stored in the memory addresses from 1008 to 1015 (i.e. 8 bytes on from 1000). Incrementing pointers is dependent on the size of addresses, not on the size of the datatype being pointed at.
On a 64 bit machine int main() { int *p; int a=10; p=&a; printf("%d ",a); printf("%p ",p); printf("%p ",p+1); printf("%d ",sizeof(int)); return 0; } Output is 10 0061FF28 0061FF2C 4 Why incrementing the pointer shift the address to only 4 bytes and not 8 bytes as you suggested?
Hi Gurpreet. I have to eat a bit of humble pie here. I was wrong. You are right. Adding 1 to a pointer like you have done in the program above gives a memory address which is the memory address stored in the pointer + sizeof(whatever datatype the pointer points to). In this case since p is pointing to an integer of 4 bytes in size the address pointed to is 4 bytes past the address of variable a. If p was pointing to a char of 1 byte in length then adding 1 would point to an address one byte along from whatever address it was pointing to. #include int main() { int *p; int a=10; p=&a; printf("%d ",a); printf("%p ",&a); printf("%p ",p); printf("%p ",p+1); // calculates 1*4 and adds it to p printf("%p ",p+10); // calculates 10*4 and adds it to p printf("%d ",sizeof(int)); // integer is 4 bytes in size printf("%d ",sizeof(p)); // pointer is 8 bytes in size but irrelevant return 0; } 10 0x7ffe14f30434 0x7ffe14f30434 0x7ffe14f30438 0x7ffe14f3045c 4 8
Wow, I understood about pointer's very well. Thanks. But I really did not understand why *(p+1) did not print 11 but prints some garbage value. Could you please answer my question? .
Summary - int a=10; int *p=&a; p is pointer to integer &a and p will give the address of a variable (suppose its 2000) *p(value at p) and a will give the value of a that is 10. (p+1) will give 2004 as output coz will give the next address which will be after 4 bytes or 2 bytes , depending how much memory integer is taking , you can find it by using - sizeof(int) *(p+1) will give the value stored at next 4 bytes which is mostly garbage value
These explanations are excellent, you're putting my professors to shame
Christian Abbott Thanks a lot :)
I learned more from his videos in one day than from 1 semester of college.
Exactly!
Mine too.
@@mycodeschool which compiler are you using?
You taught this concept better than how it was taught in Harvard's introduction to computer science class! thank you :)
cs50 girl :)
Are you Harvard girl? )
Olivia Townsend , Are you Harvard's student?
@@k.osiyuk lmao
Title are just titles. We are men of titles. We shouldn't be.
I know it sounded so philosophical!
This is not "Playing with Pointers" like you said, this is an excellent in depth explanation on C Pointers.
Thank you so much!
but he dead in car accident
Hey you are seriously gifted man... I honestly wish I was paying you my tuition and not my school. Like the other comment said you are putting my professors to shame. Keep what you are doing man... you are great at it.
Yeah ,You are right
Thank u for your simple english and great explaination!
These lectures are pure GOLD. Best lectures ever uploaded on RUclips. HatsOFF man........ :)
but he dead in car accident
@@shivamupadhyay3960 sadge
@@shivamverma9447 he was top coder in 2007 to 2014 in whole world
Man, you're an amazing teacher. I was fed up at college when i wasn't able to understand anything about pointers and your videos helped me immensely to get those stuff. Keep up the good work man. Hats off to you!
Short and concise, you my friend are legendary at teaching C.
Your videos are SO much clearer than the explanations our professor gives. Thank you so much!
Small add up to the course:
if you are trying to compile these codes from a linux terminal, you might get an error saying " expects argument type int ". its because ,when you try to get the memory address by printf, you cannot specify it as %d as its shown in the video.
instead of that try to put %p.
example:
printf("%d
", *p); // this might works fine since the out put is an integer as its expected.
but to get the address.
printf("%p
", p); //this works for the address
Oh My God! After watching this video my thought on pointers concept is totally changed as an easy concept. Perfect Explanation which I never heard before !!!!! :-) Thanks a lot
There is no any other better explanations regarding pointers in youtube. Perfectly teaching with clear examples.
when you were explaining , you used a statement like "ADDRESS OF P".....But when we are using a pointer P, we are actually referring to the address of integer variable A rather than the address of integer to pointer P...To refer to the address of P we must use another pointer to integer (Cuz P is also a pointer of type integer or a pointer to integer) and we will store the address of pointer variable P in this variable..... :D By the way , your videos are really brilliant , the best i have seen so far ! Thanks :)
The way you spent tym in making these videos as well as those captions(subtitles), is just great. Thank you so much for spending time and making our future bright.
beautiful explanation , I was struggling to understand b4 watching Ur videos , u have made illustrated it in a very simple manner, tq .
This vid saved me from failing C programming, you sir is amazing.
You explain 100x better than my professor. so thanks a lot!
Best pointer tutorials in RUclips, keep up the good work
you sir deserve an award....thank you very much!
Thank you so much! Being a non-computer science major, I've been studying programming myself and the concept of pointer has been one of the toughest to grasp. Now it makes more sense :)
man you're very clear about the explanation which my prof couldn't do over the course of 2 classes. Thanks a bunch! Saved me from understanding the material.
You rocks mycodeschool. Its so fun and easy to do programming with your tutorials.
Explained clearly and concisely. Brilliant work on the whole series.
I can't believe I can spend so long confused by pointers and I come across these videos and it all clicks together. Thank you! (Also wonderful accent, it's like the diet version :))
You're a legend bro! I hated dealing with pointers before coming across your playlist, now they seem sensible. Thank you so much! You have brought me joy, and I look forward to making it to your data structures playlist. Best wishes, and I'm excited to see what comes next with this series on pointers.
some people said is dead, pray for him.
Yaar this is THE SIMPLEST EXPLAINATION I have seen.Thank you sooo much dude!!
Sir really i thank you very much from the core of my heart. Please make more videos on other topics of programming .
your legacy will always there ,miss u harsha sir
Im in my first semester of grad school and taking an intro to C programming class. The professor ruined this concept by not explaining it well and glossing over it. I’ve been struggling with these for 4 weeks and I’m so glad I found these explanations. Everything is coming together in my brain
Sir, first of all - hats off
You’re doing an amazing job. There is one correction however, sizeof is not a function, it’s an operator which is executed at compile time.
thanks to your contribution to understanding basic logic in C programming. it is really easy to understand clearly.
THNAK YOU SO MUCH FOR THESE VIDEOS...FAR BETTER THAN UTILISING MY PRESCRIBED TEXTBOOK. YOU REALLY DONT KNOW HOW MUCH IT IS APPRECIATED. THANK YOU
I totally love the videos and teaching here :) thanks for making more people educated
I do not get your question. Garbage collection is freeing dynamically allocated memory (allocated using malloc or new operator) that is not being used at any point. Whats the relation here?
Excellent Explaination , You cleared almost every doubt anyone can have with such ease , just Mind Blowing
Thank you for the videos. Your explanations make pointers much easier to understand. I have a question, though. Why use pointers to manipulate the int value. Wouldn't it be easier to just change the value directly. I guess I thought pointers were used to copy the integer value so it could be manipulated without changing the original variable value.
Your videos are cool bro. Although from ten years ago but they can help me get more knowledge for programing
Thanks a lot. The lesson is so much detail and step-by-step. Great.
It was very helpful and professionally ! Thanks for your efforts.
Thank u sir u have made my concepts so clear may God bless ur soul...
Salam alykom
I find your lessons simple and clear
thank you
Damn this is the greatest tutorial I have ever seen!!! Thank you.
You sir just made my day.
Thank you for your contribution. I hope I could repay you some day.
U can't he is dead.
Thank you for all your hard work professor ,i think you are the next steve jobs....
Your explanations are very easy to understand. Thanks a bunch! I just subscribed!
Perfect examples! I learned a lot. Thank you.
Excellent teacher. Your videos are pure gold. Thank you ...!
May God Bless your Soul RIP brother. You are missed a lot!!!
really?
Can't believe I actually understand this! Thanks
The best explanation I have ever ever seen all my life, thumbs up ~!
you´re honestly the best account, thank you so much!!!
I have joined NIIT for c language but i couldn't get this level of explanation.
very gud explanation
Awesome work thanq :)
Fixed it.. Dude, u are awesome. Your videos help me to understand about pointers.
Thank you for all your hard work professor :)
these are really incridble videos.these videos are what i m seeking for..thanks for your work..these are helpful .
Excellent work! Much appreciated.
mano, ninguém supera a didática desse cara!!
Wow. I did not know you can initialize the "*p" to the address. My book always did the long way. Now I understand how you can pass addresses through the parameter of other functions. Wow thank you for clearing that up. I was getting confused because I thought "*p" only contained a number at which "p" had the address of. But now I see that *p is to tell the c system the the variable p is a pointer!! And omg it makes so much sense
Correct me if I'm wrong!! Please I love to learn my own misconceptions
dude can you do a data file handling tutorial cuz your tutorials are epic
same request here
He is not there anymore, word is that he passed away in an accident.
@@cheems08213 For real?
I did a research on this topic. What I found is that the narrator of the video Animesh is alive and well but the co-founder of mycodeschool who went by the nickname HumbleFool passed away somewhere in 2014-2015 in a car accident. Harsha (HumbleFool) was India's one of the best competitive programmer. Animesh (an alumni of the Indian Institute of Technology) co founded the mycodeschool with Harsha somewhere in 2009. I also read in an article that Animesh is currently working in Google. However I cannot understand why Animesh has stopped uploading videos, maybe it is because of the sudden demise of his founding partner.
(I googled all this so nothing is official and I could be wrong)
sir as u said in last *(p + 1) are not printing garbage. it's printing 2750832 that is increase address of a by 4 byte. its working fine.
Hi, thanks for the great video. In the example at 6:09, I think the printf statement should read ("Address of a is %d
", p) instead of ("Address of p is %d
", p). p is pointing to a, and by printing p we're showing the address of a, not the address of p.
At the 6:24 point of this video, the 1st printf statement("address of p is %d",p) which turned out to be 3144576 there should probably be ("address of a is %d,p) ,shouldn't it be?
Similarly,the 2nd printf("Value at p is %d,*p) printing 10 should (as far i could understand) ("Value at the address stored in p is %d",*p).
Kindly,clarify.
Thanks.
that also had me confused
correct
Actually he meant the address that pointer p points to not the actual address of pointer p
You are correct!
At 6:24 could also be: printf("value of p is %d" ,p)
I think our teacher got a little confused himself.
Anyway I think he has the best pointer lessons on RUclips
dont be confuse. see p=&a learn to read it like this. left to right. address of a is assign in pointer variable p.
*p will print value of a and p will print address. remember "HOW TO READ". address of a is assign in p. hope u understand.
lol i saw now u posted 2 years back
lol
any how
Thanks a lot for making all of these videos! You"re great explaner :)
u r great.born to teach and learn new things. at 7:45 u cleared my confusion.
thanks a lot.
great.....!! thank you so much for all the videos....can u plz upload the videos of java nd c++ too...
Thank you so much for these videos! They are really great :)
Omg. That is so fascinating. I am enthralled by pointers now.
your teachings are directly attacking my mind;)Gr8 Work
Hey I have a doubt what happens if I print a pointer with a prefix operator I mean like printf(“%d”,++(p)); when I actually run this code I am getting output with actual address minus 4
You are an excellent teacher!
Thank you very much for the explanation!
Tip: sound level is very low.
If he would have been alive the whole programming would have been a lot easy with these awesome explanations.
I think that instead of "Address p is" you could have gone with "Address stored in pointer variable p" or "Address pointed to by p" so that beginners could avoid confusion with "Address of p". (Take it from me as someone who for a long time could not understand pointers because of how people and books taught the topic.) Otherwise, great video.
Excellent Explanation , dear friend , Thank you very much for sharing ...
I am using GCC compiler by GNU and here's the output I get after executing the code:
The address of a is 0x7ffee3ebfa0c
The value at address p is 10
The size of the integer is 4
The address of value next to a is 0x7ffee3ebfa10
The value at address p+1 is -471074272
Also, to find the value at address p+1, you have to enclose the variable within brackets as (p+1) before the pointer operator, else the compiler increments the value by one unit, i.e., it returns 11.
Very very good teaching skills.great way of teaching.. why havent you covered other topics like arrays, storage classes etc..
Next level explanation....Thanks very much...
Dude, i love your videos and i want to tell u i hope u get the same help u r proving us in ur advanced career topics by other cool guys like yourself:))
hello, Admin. Thank you very much. *(p+1) will give us some garbage value. but *p +1 will increment the value that p points to 1. However, my question is will the contents that p is pointing to shall change?
In 6:21
Did you mean to write in the printf statements that the address of a is the value of p? because If I understand it correctly, what you showed was the stored value in p, which is the address of a, when you wrote that the address of p is the value in the pointer p.
*My takeaways:*
*& vs *:*
int a=10; \\ declare a variable
int *p; \\ create a pointer, and we have to initialise it otherwise we will have an error
p = &a; \\ initialise the pointer 'p' with the memory address of 'a' (i.e. &a)
int *p = &a; \\ we can also create a pointer and initialise it in one line of code
int* p = &a; \\ this does the same as the above code
*Be aware when we are not creating a pointer, 'p' is the pointer (i.e. the memory address) and '*p' is the value at the memory address pointed by 'p'.*
For example:
'*p' is the value at the memory address of 'a' (i.e. '&a')
=the value of 'a'
=10
*p = 12; \\ this is called dereferencing, we can change the value of 'a' to 12 using the pointer
*Index memory*
p is the memory address of 'a'
p+1 is the memory address of 'a' + the size of 'int' (i.e. 4 bytes), and if 'a' is in other data types, the size is different
Isn't *p = 12 called dereferencing? That is, changing the value at a position using pointers?
@@RM-lb7xw You are right, fixed.
Thank you so much, I was so confused you have solved my all doubts
You explain and teach well. Lucky I found this vid.
Thanks for your very helpful videos
You sound like Dhoni... :D .. Good Explanations :)
Thanks a lot ...
i learn so much from this video
This is one dangerous thing about C, using pointers you can access a memory location that you are not supposed to access. As long as you are reading whats written there, its fine, but if you trying to write something... you may be over-writing something critical. So program will either crash or give some unexpected behavior. Write to a memory location which is assigned for some variable, not anywhere.
Thanks aarb17!
10:54 he sounds so passionate I love it
That's hell lot of a job brother, well done.
Nice to revisit these old videos
Hlo Sir...
Like ur way of teaching very much...Superb job...u r doing over here...
After listening ur 1 or 2 lectures i m nw bit comfortable with pointers. :)
I hve a small query
" U r repeatedly using %d to print the address. But when i use it in my C program it is giving me a -ve number. But if i use %u then it gives me the right address that the pointer points to" Why the two values are different.?
Kaur Savvy try declaration as "unsigned int a=10".it wont give -ve sign with %d. %u means unsigned that's why it drops the minus sign.
*p should print the value and not the address .. When we are printing p, we are trying to print the address... Your compiler is not allowing that because its expecting an int for "%d" ,,, try printf("%p
", p).. "%p" means you are trying to print an address.
i really appreciate you man ! excellent explanation !
You explain in a lucid manner.
Dear mycodeschool and listeners! could you explain me more about your code
"printf("Address of P is %d
", p);" (at 6:22) in time.
as my understanding, this command will display the address of available (a) that is pointed by pointer P;
I think if we want to display the address of P we should use
printf("Address of P is %d
, &p");
very happy and thank so much for your lectures!
Thank you for your clear explanation.
Nice video but there is a serious error at 8:28 where you say that because the size of an integer is 4, then p+1 is 4 bytes further on from p. It's correct that it's 4 bytes further on in this case, but not because it's the size of an integer (which for the compiler you are using just happens to be 4). It's the number of bytes needed to store a memory address, which in this case also happens to be 4. If you change the data type to char, which has a size of 1 byte and the pointer + 1 would still be 4 bytes on. Also at 6:21 where you have "printf(address of P")". It's not actually the address of p that you are printing but the address stored at p, which is the address of a. Also, if you want to print memory addresses the proper way to do it is to use %p as the format string and (void*) &p as the argument. This last point is probably beyond the scope of the video but you run the risk of getting errors if you treat a memory address as an integer. .
Every byte in memory has an address. So, if you have an int that's 4 bytes and its address is 1000, 1001 is actually the 2nd byte of that int and 1002 is the third byte and 1003 is the fourth. Since the size of an int might vary from compiler to compiler, it is imperative that when you increment your pointer you don't get the address of some middle point in the int. So, the job of figuring out how many bytes to skip, based on your data type, is handled for you and you can just use whatever value you get and not worry about it.So as you were saying for a character the (p+1) will refer to next byte as char type only takes on byte.So in a nutshell pointer arithmetic doesn't work in bytes but in terms of sizeof(pointer base type)-bytes sized blocks.
I agree with what you are saying in the statement above.
What I am saying is that it is ambiguous in the video when you use 4 byte addresses and a datatype that uses 4 bytes in the same example. It becomes unclear whether the the fact that the pointer plus 1 is 4 bytes on is because of the size of the 4 byte integer datatype or because you have a machine that uses 4 bytes for addresses.
The reason why p+1 is 4 bytes further on is because in the video you are using 4 byte addresses. For example if you had a 64 bit machine and a 4 byte integer datatype then addresses i.e. pointers would occupy 8 bytes and each item of data would occupy 4 bytes. So the address at 1000 to 1007 would point to the 4 byte integer a. Incrementing the pointer to p + 1 would be to use the 8 byte address stored in the memory addresses from 1008 to 1015 (i.e. 8 bytes on from 1000).
Incrementing pointers is dependent on the size of addresses, not on the size of the datatype being pointed at.
On a 64 bit machine
int main()
{
int *p;
int a=10;
p=&a;
printf("%d
",a);
printf("%p
",p);
printf("%p
",p+1);
printf("%d
",sizeof(int));
return 0;
}
Output is
10
0061FF28
0061FF2C
4
Why incrementing the pointer shift the address to only 4 bytes and not 8 bytes as you suggested?
No problem Bernard. These types of discussions should help the viewers.Peace.
Hi Gurpreet. I have to eat a bit of humble pie here. I was wrong. You are right. Adding 1 to a pointer like you have done in the program above gives a memory address which is the memory address stored in the pointer + sizeof(whatever datatype the pointer points to). In this case since p is pointing to an integer of 4 bytes in size the address pointed to is 4 bytes past the address of variable a. If p was pointing to a char of 1 byte in length then adding 1 would point to an address one byte along from whatever address it was pointing to.
#include
int main()
{
int *p;
int a=10;
p=&a;
printf("%d
",a);
printf("%p
",&a);
printf("%p
",p);
printf("%p
",p+1); // calculates 1*4 and adds it to p
printf("%p
",p+10); // calculates 10*4 and adds it to p
printf("%d
",sizeof(int)); // integer is 4 bytes in size
printf("%d
",sizeof(p)); // pointer is 8 bytes in size but irrelevant
return 0;
}
10
0x7ffe14f30434
0x7ffe14f30434
0x7ffe14f30438
0x7ffe14f3045c
4
8
love this guys explanation
Wow, I understood about pointer's very well. Thanks. But I really did not understand why *(p+1) did not print 11 but prints some garbage value. Could you please answer my question?
.
Summary -
int a=10;
int *p=&a;
p is pointer to integer
&a and p will give the address of a variable (suppose its 2000)
*p(value at p) and a will give the value of a that is 10.
(p+1) will give 2004 as output coz will give the next address which will be after 4 bytes or 2 bytes , depending how much memory integer is taking , you can find it by using - sizeof(int)
*(p+1) will give the value stored at next 4 bytes which is mostly garbage value