Thread: How to use of const keyword with pointers?

  1. #1
    Registered User Kyle Flores's Avatar
    Join Date
    Nov 2016
    Posts
    7

    Post How to use of const keyword with pointers?

    Hi, i am learning C++ programming and I want to figure out the use of const keyword with pointers. I have explored many programming forums to get a satisfactory solution but didn't get it. I hope some of the tech experts in this community can help to figure out my issue.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As you would with any other type.

    There are three variants:
    1) const T* p
    2) T* const p
    3) const T* const p

    1) says the pointer points to const. Thus:

    int x;
    const T* p = &x;
    p = &x; // OK - reassign contents of variable
    *p = 0; // Error - can't change value of what it points to

    2) says that the variable itself is const. Thus:

    int x;
    const T* p = &x;
    p = &x; // Error - can't change value of the variable
    *p = 0; // OK - can change value of what it points to

    3) is basically a combination of both.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const keyword.
    By Aslaville in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2013, 11:50 PM
  2. Array of const pointers to const struct
    By albundy in forum C Programming
    Replies: 4
    Last Post: 08-24-2011, 09:16 PM
  3. question about const pointers and const ints
    By WarDoGG in forum C Programming
    Replies: 9
    Last Post: 01-08-2011, 02:11 PM
  4. Usages of const keyword
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 09-16-2007, 11:45 PM
  5. c++ Pointers and the const keyword
    By ncallaway in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2006, 05:07 PM

Tags for this Thread