Pointer Operators


Pointer is the memory address of some objects. A pointer variable is a variable that is specifically declared to hold a pointer to an object of its specific type. Knowing the address of a variable can be very helpful in certain types of routines. However, pointers in C / C ++ have three main functions. They can provide a quick way to reference array elements. They allow functions to modify their calling parameters. Lastly, they support linked lists and other dynamic data structures.

The first pointer operator is &, a unary operator that returns the memory address of its operand. For example,

a = &count;

places into 'a' the memory address of the variable count. This address is the internal location of the variable's computer. It has nothing to do with the value of the calculation. You can think of & as meaning "the address of." Therefore, the preceding assignment statement means "a receives the address of count."

To better understand this assignment, assume that the variable count is at memory location 1000. Also, assume that the count has a value of 50. Then, after the previous assignment, 'a' will have the value 1000.

The second pointer operator is *, which is the complement of &. The * is a unary operator that returns the value of the variable located at the address that follows it. For example, if 'a' contains the memory address of the variable count,

b = *a;

places the value of count the 'b'. Now, 'b' has the value of 50 because 50 is stored at location 1000, the memory address that was stored in 'a'. Think of * as meaning "at address." In this case, you could read the statement as "b receives the value at address a."

Unfortunately, the multiplication sign and the "at address" sign are the same, and the symbol and bit "address of" for bitwise are the same.

These operators have no relationship to each other. Both & and * have higher precedence than all other arithmetic operators except the unary minus, with which they share equal precedence.

Variables that have a memory address must be declared in front of the name of the variable. This indicates to the compiler that it will hold a pointer. For example, to declare 'c' as a character pointer,

char *c;

Here, 'c' is not a character but a pointer to a character - there is a big difference. The type of data that a pointer points to, in this case, char, is called the base type of the pointer. However, the pointer variable itself is a variable that holds the address to an object of the base type. Thus, a character pointer is of sufficient size to hold any address as defined by the architecture of the computer. However, as a rule, a pointer should only point to data that is of that pointer's base type.

Example of pointer operator

Program
#include <stdio.h>

int main() {
  int target, source;
  int *a;
  source = 10;
  a = &source;
  target = *a;
  printf("%d",target);
  return 0;
}