1. C++ Tutorials

What Is Pointers In cpp

A pointer is an expression that represents both the address and sort of another object.
Using the address operator, &, for a given object creates a pointer there to object.

Given that var is an int variable,is the address of the int object in memory and thus a pointer to var. A pointer points to a memory address and simultaneously indicates by its type how the memory address can be read or written to.

Thus, depending on the type, we refer to pointers to char, pointers to int, and so on, or use an abbreviation, such as char pointer, int pointer, and so on.

&var // Address of the object var

What is Pointer Variables

An expression such as &var is a constant pointer; however, C++ allows you to define
pointer variables, that is, variables that can store the address of another object.

int ptr; // or: int ptr;

This statement defines the variable ptr, which is an int* type (in other words, a pointer
to int). ptr can thus store the address of an int variable.

In a declaration, the star character * always means “pointer to.” Pointer types are derived types. The general form is T*, where T can be any given type. In the above example T is an int type.

Objects of the same base type T can be declared together.

int a, *p, &r = a; // Definition of a, p, r

After declaring a pointer variable, you must point the pointer at a memory address. The
program on the opposite page does this using the statement.

ptr = &var;.

How to Using Pointers to Access the Objects

The indirection operator * is used to access an object referenced by a pointer: Given a pointer, ptr, *ptr is that the object referenced by ptr.

As a programmer, you must always distinguish between the pointer ptr and the
addressed object.

long a = 10, b, // Definition of a, b

*ptr; // and pointer ptr.

ptr = &a; // Let ptr point to a.

b = *ptr;

This assigns the value of a to b, since ptr points to a. The assignment b = a; would
return the same result. The expression *ptr represents the thing a, and may be used
wherever a could be used.

The star character * used for defining pointer variables is not an operator but merely
imitates the later use of the pointer in expressions. Thus, the definition.

long *ptr;


The indirection operator * has high precedence, just like the address operator &. Both
operators are unary, that is, they have only one operand. This also helps distinguish the
redirection operator from the binary multiplication operator *, which always takes two
operands.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Do you like Zubeen's articles?  Follow on social!
Comments to: What Is Pointers In cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password