The difference between a constant pointer and a pointer to a constant in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
Пікірлер: 55
@M3t4lik Жыл бұрын
These tutorials are the best ever; practical and to the point where you can clearly see what's going on an how it all actually works with good explanation and without the clutter that most other tutorials drag out for hours. Thank you :)
@PortfolioCourses Жыл бұрын
You’re very welcome! :-) And thank you for sharing all that positive feedback, I’m really glad you enjoy the style of the videos!
@astra88155 ай бұрын
Im a little late here but your videos about pointers have helped a ton. Previously they were like eldritch magic to me, I was casting the pointer spells but the forces behind them were beyond my comprehension, but now I have a much more solid understanding of them. Thanks!
@samirprostov5554 Жыл бұрын
I really hope u gonna keep doing these great turorials. Thank u very much!
@mehmet22475 ай бұрын
A very clean explanation, thank you.
@jonathanmoore5619 Жыл бұрын
Great video. Very clear. The old read right to left rule.
@PortfolioCourses Жыл бұрын
You're welcome Jonathan, I'm glad you enjoyed it! :-)
@w1nt3rmut32 Жыл бұрын
Crystal clear explanation. Thanks.
@PortfolioCourses Жыл бұрын
You're welcome, I'm glad you enjoyed it! :-)
@yichengao101010 ай бұрын
Thanks for the video!
@PortfolioCourses10 ай бұрын
You're welcome! :-)
@qcnck27762 жыл бұрын
Very nice video. I think the problem with C pointers is the syntax of C and the re-use of the same symbol to mean 2 different things. And that one needs to read pointers right to left. Doing that makes it a little more understandable
@PortfolioCourses2 жыл бұрын
Thank you! :-) And I agree re-using the symbol for two different things does make it more confusing!
@oysteinsoreide43235 ай бұрын
In C++ you have another way of making a const pointer, it is called a reference & , but you could achieve the same with a constant pointer as well. immutable pointers are usually not much used in C though. Having the value immutable is more common use.
@gokul419016 күн бұрын
nice explanation
@relativenormality5 ай бұрын
Good video - this concept has important ramifications in an embedded environment. Microchip cover this in their tutorials.
@ChrisNoesen4 ай бұрын
my head hurts! Great videos but I am going to have to watch this about 10x. I have kept up with all the other videos just fine but this one.....well, trying to wrap my head around this one is tough.
@ropersonline4 ай бұрын
4:49: This is probably too obvious for an experience programmer to even notice as a problem or question, but at this point it would have been helpful to point out that while the value of *pointer_to_const cannot be changed directly, in this example that ostensibly constant content could still be changed by simply changing char b, like so: b = 'z'; A further line of printf("*pointer_to_const: %c ", *pointer_to_const); would then print the changed const value just fine.
@tomassauce4765 Жыл бұрын
These videos are very helpful and you explain concepts very well, however, do you think it would be possible to also draw out what you are explaining? Like how it behaves on the stack and in memory (I am still learning so I may have used these terms incorrectly) but I hope you understand what I mean. Personally, I am a better visual learner so if you were to draw out the memory it would help reinforce the subject to people like myself. Thank you for all your help so far tho!
@PortfolioCourses Жыл бұрын
I'm glad you're enjoying the videos! :-) And thanks for the feedback, I hear you for sure that some people learn differently than others. In some videos I do "drawings" of things like the stack or what's happening in memory or with some data structure, but it depends on the topic.
@Gsudi5 ай бұрын
Nice Tutorial. I was missing one thing. I would assume, that the value of a pointer to a constant is only immutable, when accessed through this pointer. So you could still change the value when it's accessed through another pointer (without the const keyword) or directly through the variable. Is this correct?
@tdoc666___5 ай бұрын
i think you can't, because a pointer to another pointer still is a pointer to a location, if it is constant, no matter how many nested pointer you have, it will just be the same... haha :)
@Gsudi5 ай бұрын
@tdoc666___ I don't mean nested pointers. Char const * ptr. *ptr is immutable. But char * ptr2 = &*ptr; Is *ptr2 mutable?
@juliopchile4 ай бұрын
Why can you use pointer_to_const to point to a non const char? Is it that it will treat it as a const even though char a was defined originally as a just char? In summary, the const behavior is up to the pointer?
@HippityhoppityGnW3 ай бұрын
Dereferencing will treat the character as a constant, but you can still edit the value through the direct value.
@BetelgeuseX8004 ай бұрын
It is only compiler instructions? Or this memory is located in special area?
@PortfolioCourses4 ай бұрын
It’s ultimately the compiler that’s enforcing the behaviours by the code it produces.
@BetelgeuseX8004 ай бұрын
@@PortfolioCourses Thank you :) Then it's like enforcing users when they are using my peryfieial drivers for example. I need to add it to my stm32 environment.
@RebelliousX4 ай бұрын
The trick is to read the statement from right to left then all will make sense.
@komalshah15355 ай бұрын
Can you provide practical use of this in different situations in real life programming?
@oysteinsoreide43235 ай бұрын
If you want to control immutability, then this is very valuable. As if you have something immutable, you know that what you have, is never changed by anything. So if you have an important value in your program that you don't want anyone to mess with, making them immutable, makes it easier to know that the program behaves as intended. mutable pointers or pointers to pointers are good for either returning values, or newly allocated memory. Mutable pointers are much used in different copy functions, while in places you only need to read the pointers are places like printing to the console. And real life is a very wide term by the way. You can have a real world system with millions of lines with code, and real world systems with only hundreds. I don't think I have every used "const T * const ptrvar; " at any point. But since I write mostly C++ code, then I use a reference instead: "T const & refvar ;" which is equvalet to "T const * const ptrVar;"
@komalshah15355 ай бұрын
I find C programming concepts more difficult to comprehend than philosophy! C is remarkable obtuse language.
@oysteinsoreide43235 ай бұрын
@@komalshah1535 c is a quite small language compared to C++ for instance. It's much more complex to understand all things that you can do in C++ than C.
@retrotechnerd10 ай бұрын
I understood the first 15 seconds of this video perfectly!
@Raam1827 Жыл бұрын
If I declare like this const char const *p; What happen???
@PortfolioCourses Жыл бұрын
Great question Raghu! :-) Then you will have a constant pointer to a constant.
@Raam1827 Жыл бұрын
@@PortfolioCourses what is the difference between const char *const p and const char const *p.
@fuzzy-02 Жыл бұрын
So a pointer to a constant, cant we just write const char b?
@PortfolioCourses Жыл бұрын
Good question, I'll try my best to help. :-) const char b would be a constant char variable, i.e. a char variable we cannot change. Constant pointers and pointers to constants are different concepts, they both involve a pointer in different ways but const char b does not.
@fuzzy-02 Жыл бұрын
@@PortfolioCourses so, in the video, we couldnt change b through dereferencing the pointer_to_constant. But does that mean that we can change b without using a p_to_constant? Like b = 'z'; I think ill go try to experiment. Thank you for the video, my professor just reads stuff but you actually explained it.
@PortfolioCourses Жыл бұрын
Yes, we could change b like that. :-) And you're welcome!
@y_x25 ай бұрын
You forget to mention what you can do with a constant pointer...
@hansdampf22845 ай бұрын
I hate this a little. Why is is not like you say it in English and like you would declare the variable if it would be a pointer. const char = constant char char* = pointer to a char const char* const pointer to a char *Const char = pointer to a Const char That would be way less confusing
@Jonas-Seiler4 ай бұрын
how did this get a pass. why did nobody prevent this atrocious design.
@undercrackers566 ай бұрын
I was wondering why you are talking about automobiles. I then realised you are mispronouncing "char" as "car". How strange.
@PortfolioCourses6 ай бұрын
I’m not mispronouncing “char”, it’s not a real word so people tend to pronounce it in a few different ways and that is one of them: english.stackexchange.com/a/60175. :-)
@hansdampf22845 ай бұрын
Cars belong to lisp
@dimi1445 ай бұрын
The letter combination "ch" can be either pronounced as just "c", like in the word "chromosome" or as "ch" as in "chocolate". When looked at standalone, "char" feels like it should be pronounced with the latter sound, but it could also be looked at as a part of the word "character" where "ch" is pronounced as "c". Although, if it was like that "char" would sound more like "care". But anyway the point is you can pronounce it as "car", "char", "care" or whatever you want, since it's not an actual word
@johnabrossimow5 ай бұрын
Char pronunciation: ❌ CHar, as in charcoal ❌ Kar, like car ✅ Kerr, as in character. Because char is the damn abbreviation of character.
@jackgerberuae2 жыл бұрын
Videos like this is only making understanding pointers more difficult. For the others, it might be helpful 😫😫😫
@PortfolioCourses2 жыл бұрын
This is a more niche and advanced topic Jacques, so don’t worry if it doesn’t make sense right away if you’re still learning about pointers in general. :-)