Constant Pointer VS. Pointer To A Constant | C Programming Tutorial
Вставка
- Опубліковано 9 лют 2025
- The difference between a constant pointer and a pointer to a constant in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
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 :)
You’re very welcome! :-) And thank you for sharing all that positive feedback, I’m really glad you enjoy the style of the videos!
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!
Read the declaration inside out. "char *const constant_pointer;" declares a variable named "constant_pointer" whose value is constant and, when dereferenced, returns a char. "char const *constant_pointer" declarers a variable named "constant_pointer" whose value, when dereferences, return a constant value which is a char. So "const char *constant_pointer" would be equivalent to the latter: a variable whose value, when dereferenced, returns a char which is constant.
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
Thank you! :-) And I agree re-using the symbol for two different things does make it more confusing!
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.
I really hope u gonna keep doing these great turorials. Thank u very much!
Great video. Very clear. The old read right to left rule.
You're welcome Jonathan, I'm glad you enjoyed it! :-)
A very clean explanation, thank you.
Good video - this concept has important ramifications in an embedded environment. Microchip cover this in their tutorials.
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.
Crystal clear explanation. Thanks.
You're welcome, I'm glad you enjoyed it! :-)
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.
The trick is to read the statement from right to left then all will make sense.
Thanks for the video!
You're welcome! :-)
nice explanation
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!
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.
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?
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 :)
@tdoc666___ I don't mean nested pointers.
Char const * ptr.
*ptr is immutable.
But char * ptr2 = &*ptr;
Is *ptr2 mutable?
Thanks!
what about char *const *s ?
Read inside out: you declare s to be a variable whose value, when dereferenced, is a constant value which, when dereferenced, is a char.
So the value of s will be a pointer to a constant pointer to a char.
Why not use static in the constant pointer case?
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?
Dereferencing will treat the character as a constant, but you can still edit the value through the direct value.
It is only compiler instructions?
Or this memory is located in special area?
It’s ultimately the compiler that’s enforcing the behaviours by the code it produces.
@@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.
Can you provide practical use of this in different situations in real life programming?
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;"
I find C programming concepts more difficult to comprehend than philosophy! C is remarkable obtuse language.
@@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.
I understood the first 15 seconds of this video perfectly!
You forget to mention what you can do with a constant pointer...
If I declare like this
const char const *p;
What happen???
Great question Raghu! :-) Then you will have a constant pointer to a constant.
@@PortfolioCourses what is the difference between const char *const p and const char const *p.
So a pointer to a constant, cant we just write const char b?
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.
@@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.
Yes, we could change b like that. :-) And you're welcome!
Don't change that 999
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
Just read from right to left?
how did this get a pass. why did nobody prevent this atrocious design.
You said the same thing soooo many times.
Research shows repetition is important for learning. :-)
I was wondering why you are talking about automobiles. I then realised you are mispronouncing "char" as "car". How strange.
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. :-)
Cars belong to lisp
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
Char pronunciation:
❌ CHar, as in charcoal
❌ Kar, like car
✅ Kerr, as in character. Because char is the damn abbreviation of character.
Videos like this is only making understanding pointers more difficult. For the others, it might be helpful 😫😫😫
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. :-)