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.

 

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"...

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.

 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...

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...

 

ptr = &a ;

In the above statement, the address of variable "a" is assigned to pointer variable "ptr". 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...

*pointerVariableName

 Example

 #include<stdio.h>
#include<conio.h>

void main()
{
   int a = 20, *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) ;

 getch();
}

Output 

 

 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.

No comments:

Post a Comment