pointer in c++ part1

Post on 24-Dec-2014

1.419 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Author – Subhasis Nayak

A pointer is a variable that holds amemory address. That’s it.

This is what the difference in betweenvariable and pointer.◦ Pointer holds the address

◦ Variable holds the value.

Computer memory is divided intosequentially numbered memory locations.Each variable is located at a unique locationin memory, known as its address.

Memory representation

The capability to use pointers andmanipulate memory at a low levelis one of the factors that makesC++ the language of choice forembedded and real-timeapplications.

Different computers number this memoryusing different complex schemes.

We must happy, as a programmer, We don’tneed to know the particular address of anygiven variable because the compiler handlesthe details.

If we want this information, though, you canuse the address-of operator (&), whichreturns the address of an object in memory.

It is so simple to declare a pointer. We needthe type of the pointer

Next the name of the pointer.

When we declare a pointer variable such aspointer, the compiler sets aside enoughmemory to hold an address

int *pointer;

A pointer that is not initialized is called a wildpointer because you have no idea what it ispointing to.

If you want to initialize you can as like we dofor variable. int *intPointer = 0;

A pointer whose value is zero is called a nullpointer.

All pointers, when they are created, should beinitialized to something.

Every variable has an address. Even withoutknowing the specific address, you can store avariable’s address in a pointer.

Declaring pointer Passing address

Holds the address

Holds the value

Type must be same to the variable whoseaddress is going to pass to the pointer.

When you want to pass the address of thevariable use (&) before the variable name.

When you want to pass the value use (*)before the pointer.

top related