Indirection operator
Syntax to declare pointer variable data-type * pointer-variable-name Pointer variable declaration follows almost similar syntax as of normal variable. Let us declare our first pointer variable. Once you got basics of memory addresses, reference and dereference operator. * and (*(&num)) gets value pointed by (&num) Syntax to use dereference operator *memory-address-or-pointer-variable Example program to use dereference operator /** Dereference operator is also known as indirection operator. We use unary * dereference operator to get value pointed by a memory address. Once you have a memory address, you must be willing to get value stored at that memory address, for that we need to dereference the memory address.ĭereferencing is the process of retrieving value at memory location pointed by a pointer. Note: Output of above program may vary on your machine. Printf("Address of num in hexadecimal = %x", &num) * C program to get memory address of a variable Syntax to use reference of operator &variable-name Example program to use reference operator /** Read more about operators in C programming. Reference operator is also known as address of operator. We use unary & (reference of) operator to get memory address of a variable.
#INDIRECTION OPERATOR HOW TO#
Reference operator &īecause we are dealing with memory addresses, we must know how to get memory address of a variable. So from now always use the language pointer points to a memory location. Instead, we say pointer points to a memory location. Note: We never say pointer stores or holds a memory location. Which means an integer pointer can hold only integer variable addresses. Yes, every pointer variable has a data type associated with it. If it is a variable, it must have a valid C data type. Whenever you refer num inside your program, internally C refers to the memory location of num.Ī pointer is a variable that stores memory address. Finally, the constant 10 is stored at 0x1200.The label is mapped to the allocated memory. After memory allocation, the C compiler defines a label (variable name) to access the memory location.Let say memory is allocated at address 0x1200. For the above statement, the C compiler allocates memory capable to store an integer.Computer memory of 64KBīefore I formally introduce pointers let us first see what happens during a variable definition. These addresses starts from zero and runs up to maximum memory size (in bytes).įor example, memory location of a 64KB RAM starts from 0 and ends to 65536 (or 0x10000) bytes. Each cell has a unique numeric address (also known as physical memory address) associated with it.
Where individual block is called as cell (memory cell). Understanding memoryĬomputer memory ( RAM) is a collection of contiguous block of bytes.
Hence, let us first understand memory in contrast to C programming. Pointers are closely related to low level memory operations. Pointers increases execution speed of program.Which undoubtedly is the biggest advantage of pointers. Pointer allows dynamic memory allocation (creation of variables at runtime) in C.We use pointers to get reference of a variable or function.Pointers are used to return multiple values from a function.Pointers are more efficient in handling arrays and structures.Pointers separates C from other programming languages.Ĭ programmers make extensive use of pointers, because of their numerous benefits. It is the most distinct feature of C, which provides power and flexibility to C. Note : That an address of a variable should be assigned to a pointer before the indirection operator along with the pointer is used in any manipulation.Pointers are the heart of C programming. When this program is executed, the values referred by the pointers are printed as shown below : value of m = 15 The symbol * ( asterisk ) is an indirection operator which is used to access the value of a variable through a pointer. Note : The pointer maybe initialized in the declaration statement as shown below : int m = 15, *mptr = &m The symbol & ( ampersand ) is an address operator which is used to access the address of a variable and assign it to a pointer to initialize it. The pointer variable in the declaration is preceded by the *(asterisk) symbol. printf("\n Biggest number is %d",*big) Ī pointer is declared like a variable with appropriate data type./* Description: find a biggest number use pointer */.Each cell in memory is 1 byte and has a unique address to identify it. Remember that the RAM has many cells to store values. A pointer is but a variable-like name which pointer or represents a storage location in memory (RAM).