You are on page 1of 3

POINTERS

A pointer is a variable that contains a memory address as its value. It contains an address of a variable
that contains a specific value.

Syntax for Declaration:


data_type *variable_name;

Example:
int *ptr; /*means ptr is a pointer to an integer value*/

* - indicates that the variable being declared is a pointer.

The data type indicates what type of variable the pointer is allowed to point to. Thus, if a pointer has to
point to a character, the data type of the pointer should be char. For the above example, ptr can only
point to an integer variable.

Valid: int x=5, *ptr=&x;

Invalid: int x=5; char *ptr=&x;

Indirection

Indirection means referencing a value through a pointer.

Difference between directly and indirectly referencing a variable:

Pointer Operators

1. Address operator (&) – a unary operator that returns the address of its operand.

Example:

int y = 5;

int *yptr;

yptr = &y; /* assigns the address of the variable y to pointer variable yptr */
Illustration:

Since yptr holds the address of y, yptr points to y. Thus, it can indirectly access the value of y. In other
words, a pointer points to a variable if it holds the address of that variable.

2. Indirection operator (*) – also known as dereferencing operator. It returns the value of the object
to which its operand (i.e., a pointer) points.

Example:

printf(“%d”, *yptr); /* prints the value of variable y which is 5 */

To display the address of a variable or the value of a pointer, use %p conversion specification.

%p – outputs the memory location as a hexadecimal integer.

Sample Program:

Output:
Open the link below to understand pointers more.

https://www.youtube.com/watch?v=47IS8VtAM9E

You might also like