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!

КОМЕНТАРІ • 63

  • @M3t4lik
    @M3t4lik 2 роки тому +24

    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  2 роки тому +4

      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!

  • @ronald3836
    @ronald3836 5 місяців тому +2

    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.

  • @qcnck2776
    @qcnck2776 2 роки тому +10

    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 роки тому +3

      Thank you! :-) And I agree re-using the symbol for two different things does make it more confusing!

    • @oysteinsoreide4323
      @oysteinsoreide4323 Рік тому

      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.

  • @samirprostov5554
    @samirprostov5554 Рік тому +1

    I really hope u gonna keep doing these great turorials. Thank u very much!

  • @jonathanmoore5619
    @jonathanmoore5619 Рік тому +1

    Great video. Very clear. The old read right to left rule.

  • @mehmet2247
    @mehmet2247 Рік тому +1

    A very clean explanation, thank you.

  • @relativenormality
    @relativenormality Рік тому +1

    Good video - this concept has important ramifications in an embedded environment. Microchip cover this in their tutorials.

  • @ropersonline
    @ropersonline 11 місяців тому

    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.

  • @w1nt3rmut32
    @w1nt3rmut32 Рік тому

    Crystal clear explanation. Thanks.

  • @ChrisNoesen
    @ChrisNoesen 11 місяців тому

    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.

  • @RebelliousX
    @RebelliousX 11 місяців тому +1

    The trick is to read the statement from right to left then all will make sense.

  • @yichengao1010
    @yichengao1010 Рік тому +1

    Thanks for the video!

  • @gokul4190
    @gokul4190 7 місяців тому

    nice explanation

  • @tomassauce4765
    @tomassauce4765 Рік тому +2

    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  Рік тому +1

      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 Рік тому +1

    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___ Рік тому

      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 Рік тому

      @tdoc666___ I don't mean nested pointers.
      Char const * ptr.
      *ptr is immutable.
      But char * ptr2 = &*ptr;
      Is *ptr2 mutable?

  • @Hossein-c3c7u
    @Hossein-c3c7u 7 місяців тому +2

    Thanks!
    what about char *const *s ?

    • @ronald3836
      @ronald3836 5 місяців тому

      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.

  • @Barzi2001
    @Barzi2001 3 місяці тому

    Why not use static in the constant pointer case?

  • @juliopchile
    @juliopchile Рік тому

    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 10 місяців тому

      Dereferencing will treat the character as a constant, but you can still edit the value through the direct value.

  • @BetelgeuseX800
    @BetelgeuseX800 11 місяців тому

    It is only compiler instructions?
    Or this memory is located in special area?

    • @PortfolioCourses
      @PortfolioCourses  11 місяців тому +1

      It’s ultimately the compiler that’s enforcing the behaviours by the code it produces.

    • @BetelgeuseX800
      @BetelgeuseX800 11 місяців тому

      @@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 Рік тому

    Can you provide practical use of this in different situations in real life programming?

    • @oysteinsoreide4323
      @oysteinsoreide4323 Рік тому

      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 Рік тому

      I find C programming concepts more difficult to comprehend than philosophy! C is remarkable obtuse language.

    • @oysteinsoreide4323
      @oysteinsoreide4323 Рік тому

      @@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 Рік тому

    I understood the first 15 seconds of this video perfectly!

  • @y_x2
    @y_x2 Рік тому

    You forget to mention what you can do with a constant pointer...

  • @Raam1827
    @Raam1827 Рік тому

    If I declare like this
    const char const *p;
    What happen???

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      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  Рік тому +1

      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!

  • @Gokul72458
    @Gokul72458 5 місяців тому

    Don't change that 999

  • @hansdampf2284
    @hansdampf2284 Рік тому

    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

    • @ronald3836
      @ronald3836 5 місяців тому

      Just read from right to left?

  • @Jonas-Seiler
    @Jonas-Seiler 11 місяців тому

    how did this get a pass. why did nobody prevent this atrocious design.

  • @johnjoyce
    @johnjoyce 7 місяців тому

    You said the same thing soooo many times.

    • @PortfolioCourses
      @PortfolioCourses  7 місяців тому

      Research shows repetition is important for learning. :-)

  • @undercrackers56
    @undercrackers56 Рік тому +14

    I was wondering why you are talking about automobiles. I then realised you are mispronouncing "char" as "car". How strange.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +17

      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 Рік тому +1

      Cars belong to lisp

    • @dimi144
      @dimi144 Рік тому +3

      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 Рік тому

      Char pronunciation:
      ❌ CHar, as in charcoal
      ❌ Kar, like car
      ✅ Kerr, as in character. Because char is the damn abbreviation of character.

  • @jackgerberuae
    @jackgerberuae 2 роки тому

    Videos like this is only making understanding pointers more difficult. For the others, it might be helpful 😫😫😫

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +6

      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. :-)