Pointers in C Programming



Pointers is one of the excellent feature introduced in C. It makes the programming very interesting.
Talking like a layman, pointers points to an object or something.
Let us understand the pointers in detail.
Before learning the basics of pointer, let us understand how the variable is stored in the memory.
When we declare a variable and assign a value to the variable, we are actually naming the memory location at which the value will be stored. The value will be stored at the memory location and not in the variable.

We can display this address using ampersand(&) as follows:
int a=10;
printf(“%u”,&a) ; //displays the address of the variable a
printf(“%d”,*(&a)); //displays the content at that address

In the above code snippet,
Value 10 is assigned to the variable a. Internally, a is the name give to the memory address.
Using ampersand we can print the address of a as shown above.
Similarly, we can display the content at that address using *. The ‘*(&a)’ is read as “content at address of a”.
This address can be stored in a variable as follows.
b = &a;
As we all know that we should declare a variable before using it, we cannot directly use it to assign an address.
As the variable b is pointing to the memory location, there is different way to declare this variable. This declaration can be done as follows:

int *b;
b = &a;
Here, b is pointing to the memory address. Hence, b is called a pointer.
After assigning the address to the pointer variable b, we can get the value at the address at which it is pointing using *.

Let us understand the pointer in detail using an example.
Example:
void main(){
int *b, a=10;
b=&a;
printf(“\nThe address of a : %u”, b);
printf(“\nThe address of a : %u”,&a);
printf("\nThe address of a : %u",*(&b));
printf(“\nThe value of a : %d”,a);
printf(“\nThe value of a : %d”,*b);
printf(“\nThe value of a : %d”,*(&a));
}


Output:

The address of a : 65524
The address of a : 65524
The address of a : 65524
The value of a : 10
The value of a : 10
The value of a : 10

Let us understand the concept of pointers to pointer. Pointers to pointer is a pointer that points to another pointer. Here pointer will store the address of another pointer.
Let us understand this using an example.
Example:
void main(){
int *b, a=10, **c;
clrscr();
b=&a;
c=&b;
printf("\nThe address of a : %u", b);
printf("\nThe address of a : %u",&a);
printf("\nThe address of a : %u",*(&b));
printf("\nThe address of a : %u",*c);
printf("\nThe value of a : %d",a);
printf("\nThe value of a : %d",*b);
printf("\nThe value of a : %d",*(&a));
printf("\nThe value of a : %d",*(*c));
getch();
}

Output:

The address of a : 65524
The address of a : 65524
The address of a : 65524
The address of a : 65524
The value of a : 10
The value of a : 10
The value of a : 10
The value of a : 10

Demonstration on Pointer Operations


Responses

1 Respones to "Pointers in C Programming"

balajaimille said...

Caesars agrees to multi-year sponsorship deal for Caesars
The deal 삼척 출장마사지 also includes 보령 출장안마 a $50 million retail-only Caesars Sportsbook and 광명 출장마사지 a $200 million 대구광역 출장안마 resort casino and retail-only Caesars Casino 아산 출장샵 in


March 4, 2022 at 8:09 AM

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy