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
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.
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
If you like this post, don’t forget to share 🙂