The perfect place for easy learning...

C Programming Language


Topics List

Place your ad here

Pointers in C

In the c programming language, we use normal variables to store user data values. When we declare a variable, the compiler allocates required memory with the specified name. In the c programming language, every variable has a name, datatype, value, storage class, and address. We use a special type of variable called a pointer to store the address of another variable with the same datatype. A pointer is defined as follows...

Pointer is a special type of variable used to store the memory location address of a variable.

In the c programming language, we can create pointer variables of any datatype. Every pointer stores the address the variable with same datatype only. That means integer pointer is used store the address of integer variable only.

Accessing the Address of Variables

In c programming language, we use the reference operator "&" to access the address of variable. For example, to access the address of a variable "marks" we use "&marks". We use the following printf statement to display memory location address of variable "marks"...

Example Code

printf("Address : %u", &marks) ;

In the above example statement %u is used to display address of marks variable. Address of any memory location is unsigned integer value.

Declaring Pointers (Creating Pointers)

In c programming language, declaration of pointer variable is similar to the creation of normal variable but the name is prefixed with * symbol. We use the following syntax to declare a pointer variable...

datatype *pointerName ;

A variable declaration prefixed with * symbol becomes a pointer variable.

Example Code

int *ptr ;

In the above example declaration, the variable "ptr" is a pointer variable that can be used to store any integer variable address.

Assigning Address to Pointer

To assign address to a pointer variable we use assignment operator with the following syntax...

pointerVariableName = & variableName ;

For example, consider the following variables declaration...

Example Program | Test whether given number is divisible by 5.

int a, *ptr ;

In the above declaration, variable "a" is a normal integer variable and variable "ptr" is an integer pointer variable. If we want to assign the address of variable "a" to pointer variable "ptr" we use the following statement...

Example Code

ptr = &a ;

In the above statement, the address of variable "a" is assigned to pointer variable "prt". Here we say that pointer variable ptr is pointing to variable a.

Accessing Variable Value Using Pointer

Pointer variables are used to store the address of other variables. We can use this address to access the value of the variable through its pointer. We use the symbol "*" infront of pointer variable name to access the value of variable to which the pointer is pointing. We use the following general syntax...


Example Code


void main()
   int a = 10, *ptr ;
   ptr = &a ;
   printf("Address of variable a = %u\n", ptr) ;
   printf("Value of variable a = %d\n", *ptr) ;
   printf("Address of variable ptr = %u\n", &ptr) ;


switch in c programming

In the above example program, variable a is a normal variable and variable ptr is a pointer variable. Address of variable a is stored in pointer variable ptr. Here ptr is used to access the address of variable a and *ptr is used to access the value of variable a.

Memory Allocation of Pointer Variables

Every pointer variable is used to store the address of another variable. In computer memory address of any memory location is an unsigned integer value. In c programming language, unsigned integer requires 2 bytes of memory. So, irrespective of pointer datatype every pointer variable is allocated with 2 bytes of memory.

Place your ad here
Place your ad here