pointer in c++ part1

11
Author Subhasis Nayak

Upload: subhasis-nayak

Post on 24-Dec-2014

1.419 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Pointer in c++ part1

Author – Subhasis Nayak

Page 2: Pointer in c++ part1

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.

Page 3: Pointer in c++ part1

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

Memory representation

Page 4: Pointer in c++ part1

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.

Page 5: Pointer in c++ part1

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.

Page 6: Pointer in c++ part1
Page 7: Pointer in c++ part1

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;

Page 8: Pointer in c++ part1

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.

Page 9: Pointer in c++ part1

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

Declaring pointer Passing address

Page 10: Pointer in c++ part1

Holds the address

Holds the value

Page 11: Pointer in c++ part1

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.