Pointers, C/C++ Programming

When declaring a variable of data type pointer, use the * in front of the variable name. These variables hold a memory location (like B45CDF), not an actual value like 30 or A:

int * Years;

char * Grade;

This tells the program that the value contained in the variable will be a memory address that points to the value at that memory address.

To obtain an actual memory address to use with this variable, the & or reference operator is used.

In the following example, a regular variable is declared. Whenever a variable is declared in a program, the operating system sets aside a memory location in the computer to hold whatever value of a given data type is placed in that memory location. So placing the & in front of a regular variable, tells the operating system that the program wants the memory location address and not the value contained in that memory location.

int LoanYears;

int * Years;

The following code assigns the memory location for the LoanYears variable to the pointer variable.

Years = &LoanYears;

Note: If the assignment was "Years = LoanYears;" a compiler error would occur since the LoanYears is of the wrong data type that Years. Years only accepts memory locations, not actual values.

To assign a value or change the value of LoanYears using the pointer instead of the actual variable, the following code is used:

*Years = 30;

equivalent to

LoanYears = 30;

NOTE: Among the symbols expected in the code are -> and the use of ++ with the pointer variable for the pointer to the struct.

Posted Date: 4/1/2013 3:21:42 AM | Location : United States







Related Discussions:- Pointers, Assignment Help, Ask Question on Pointers, Get Answer, Expert's Help, Pointers Discussions

Write discussion on Pointers
Your posts are moderated
Related Questions
Question: (a) (i) Explain how class members can be accessed when using objects of the class. Use a suitable example to illustrate your answer. (ii) Explain how the privat

Write a program to create a binary file and store the following data "hello", 0x0030,'1', 1.234  Using visual studio examine the binary file and note how the day is stored   Answe

Damjibhai and Shamjibhai are two jeweler friends. They decide to play a simple game. The game comprises of removing the jewels for polishing, turn by turn. Once a jewel is removed

F u nction overloading: Functions can be defined with same name.  Depending upon the type of argument passed the function will perform.  This is known function overloading

1. Create text a file with the name "myemail" that has a single line in it, consisting of your email address. Something like "firstname_lastname@carleton.ca" (or your hotmail or gm

how to see inbox details of a particular email adress on console

I need Asp .net Facebook app project completion Project Description: We are urgently seeking a few remaining items to be completed, and the project to be deployed to live.

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t

#include #include using namespace std; class Book { private: string title; int iD; int year_pub; string author; public: string getTitle(); int getId(); int getYear_Pub(); st

A: Provided that function parameter is "const reference", compiler create temporary variable in following two ways. a) The actual argument is the correct type, however it isn't