ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

CPP Pointer Constants and Pointer Variables

Updated on April 21, 2009
/************************************************************************************
 * 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"
}


Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.