CPP Pointer Constants and Pointer Variables

/************************************************************************************
 * Pointer Variables and Pointer Constants                                 main.cpp *
 *                                                                                  *
 * Author: nicomp                                                           *
 *                                                                                  *
 * Abstract:                                                                        *
 * Pointer variables can be assigned a new value.                                   *
 * Pointer constants cannot be assigned a new value.                                *
 *                                                                                  *
 *                                                                                  *
 *                                                                                  *
 *                                                                                  *
 *                                                                                  *
 ************************************************************************************/

#include <iostream>
#include <assert.h>

using namespace std;

void main()
{
//    The difference is in the notation
    char *chrVariable = "Indiana";    // A Pointer Variable, initialized
    char chrConstant[] = "Ohio State";    // A Pointer Constant, initialized

//    This expression is a pointer constant: "Hello"
    
    chrVariable = "Hoosiers";        // This is OK. The original string is gone.
    chrConstant = "Buckeyes";        // This is not OK, on so many levels.
    chrConstant = "Ohio State";        // This is not OK either.

    cout << "\n\n";
}

//***********************************************************
// The difference between "const char *" and "char * const"
//***********************************************************

void MyFirstFunction(const char *chrBuff)
{
    chrBuff = "Hi";
}
void MySecondFunction(char * const chrBuff)
{
    chrBuff = "Hi";    // This won't compile: "You cannot assign to a value that is const"
}


More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working