Constant Pointer VS. Pointer To A Constant | C Programming Tutorial

  Рет қаралды 25,331

Portfolio Courses

Portfolio Courses

2 жыл бұрын

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
@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
@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!
@astra8815
@astra8815 5 ай бұрын
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
@samirprostov5554 Жыл бұрын
I really hope u gonna keep doing these great turorials. Thank u very much!
@mehmet2247
@mehmet2247 5 ай бұрын
A very clean explanation, thank you.
@qcnck2776
@qcnck2776 2 жыл бұрын
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
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-) And I agree re-using the symbol for two different things does make it more confusing!
@oysteinsoreide4323
@oysteinsoreide4323 5 ай бұрын
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.
@jonathanmoore5619
@jonathanmoore5619 Жыл бұрын
Great video. Very clear. The old read right to left rule.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Jonathan, I'm glad you enjoyed it! :-)
@w1nt3rmut32
@w1nt3rmut32 Жыл бұрын
Crystal clear explanation. Thanks.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, I'm glad you enjoyed it! :-)
@yichengao1010
@yichengao1010 10 ай бұрын
Thanks for the video!
@PortfolioCourses
@PortfolioCourses 10 ай бұрын
You're welcome! :-)
@relativenormality
@relativenormality 5 ай бұрын
Good video - this concept has important ramifications in an embedded environment. Microchip cover this in their tutorials.
@gokul4190
@gokul4190 16 күн бұрын
nice explanation
@ChrisNoesen
@ChrisNoesen 4 ай бұрын
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.
@ropersonline
@ropersonline 4 ай бұрын
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
@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
@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.
@Gsudi
@Gsudi 5 ай бұрын
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___
@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 :)
@Gsudi
@Gsudi 5 ай бұрын
@tdoc666___ I don't mean nested pointers. Char const * ptr. *ptr is immutable. But char * ptr2 = &*ptr; Is *ptr2 mutable?
@juliopchile
@juliopchile 4 ай бұрын
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?
@HippityhoppityGnW
@HippityhoppityGnW 3 ай бұрын
Dereferencing will treat the character as a constant, but you can still edit the value through the direct value.
@RebelliousX
@RebelliousX 4 ай бұрын
The trick is to read the statement from right to left then all will make sense.
@BetelgeuseX800
@BetelgeuseX800 4 ай бұрын
It is only compiler instructions? Or this memory is located in special area?
@PortfolioCourses
@PortfolioCourses 4 ай бұрын
It’s ultimately the compiler that’s enforcing the behaviours by the code it produces.
@BetelgeuseX800
@BetelgeuseX800 4 ай бұрын
@@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.
@komalshah1535
@komalshah1535 5 ай бұрын
Can you provide practical use of this in different situations in real life programming?
@oysteinsoreide4323
@oysteinsoreide4323 5 ай бұрын
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;"
@komalshah1535
@komalshah1535 5 ай бұрын
I find C programming concepts more difficult to comprehend than philosophy! C is remarkable obtuse language.
@oysteinsoreide4323
@oysteinsoreide4323 5 ай бұрын
@@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.
@retrotechnerd
@retrotechnerd 10 ай бұрын
I understood the first 15 seconds of this video perfectly!
@Raam1827
@Raam1827 Жыл бұрын
If I declare like this const char const *p; What happen???
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Raghu! :-) Then you will have a constant pointer to a constant.
@Raam1827
@Raam1827 Жыл бұрын
@@PortfolioCourses what is the difference between const char *const p and const char const *p.
@fuzzy-02
@fuzzy-02 Жыл бұрын
So a pointer to a constant, cant we just write const char b?
@PortfolioCourses
@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
@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
@PortfolioCourses Жыл бұрын
Yes, we could change b like that. :-) And you're welcome!
@y_x2
@y_x2 5 ай бұрын
You forget to mention what you can do with a constant pointer...
@jackgerberuae
@jackgerberuae 2 жыл бұрын
Videos like this is only making understanding pointers more difficult. For the others, it might be helpful 😫😫😫
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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. :-)
@hansdampf2284
@hansdampf2284 5 ай бұрын
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-Seiler
@Jonas-Seiler 4 ай бұрын
how did this get a pass. why did nobody prevent this atrocious design.
@undercrackers56
@undercrackers56 6 ай бұрын
I was wondering why you are talking about automobiles. I then realised you are mispronouncing "char" as "car". How strange.
@PortfolioCourses
@PortfolioCourses 6 ай бұрын
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. :-)
@hansdampf2284
@hansdampf2284 5 ай бұрын
Cars belong to lisp
@dimi144
@dimi144 5 ай бұрын
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
@johnabrossimow
@johnabrossimow 5 ай бұрын
Char pronunciation: ❌ CHar, as in charcoal ❌ Kar, like car ✅ Kerr, as in character. Because char is the damn abbreviation of character.
Make A Directory Using _mkdir() On Windows | C Programming Tutorial
6:13
Portfolio Courses
Рет қаралды 3,3 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
That's how money comes into our family
00:14
Mamasoboliha
Рет қаралды 10 МЛН
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 56 М.
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 66 М.
Diff between const char pointer and const pointer to char
7:15
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 287 М.
Essentials: Pointer Power! - Computerphile
20:00
Computerphile
Рет қаралды 462 М.
are "smart pointers" actually smart?
9:44
Low Level Learning
Рет қаралды 71 М.
int *p vs int* p Pointer Declarations | C Programming Tutorial
6:12
Portfolio Courses
Рет қаралды 133 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 335 М.