Introduction to Pointers:
C is a very powerful programming language that are used by many software developers to develop different kinds of software.
C is the high level, powerful programming language which one is used by many developers to develop various kind of softwares.
However, to a new learners or a beginner C is a little bit difficult language to grasp. The difficulties in learning C for beginners comes from the confusion over the concept of pointers. In this article, let explain the concept of pointers with the help of some examples.
Pointers: A pointer in c language is defined as a variable which stores the address of another variable or whose value is the address of another variable of the same type.
The & Operator is used to refere the address of the variable and the * operator is used to dereference the value of the variable that the pointers variable points to.
The C language have different types of pointers they are
void,dangling, null,near, wild,huge far. A programmer can typecast a pointers to different data types.
What is pointers in C programming?.
Variables,Addresses and Pointers:
Pointer: A pointer in C programming is a variable that stores the address of another variable in the memory variable.
It is much important to differentiate between variable, pointer and the address that the pointer holds. This is the source of much of the confusion about pointers. From the given fig.We will illustrate the code snippets.
int n= 3;
int *ptr;
ptr=&n;
In the first statement we have declared a variable “n” of type integer and assigns
a value 3 to it.
The second statement int *ptr declares a pointer variable that holds the address
of an integer variable.
The Third statement ptr=&n assigns the address of the variable “n” to pointer variable ptr.
for our understanding let study from fig.
The variable n is declared as integer and assigned value 3 and whose memory address is 2000.
After that we declared a integer pointer variable int *ptr. The pointer vaiable ptr stores the address of another integer variable.
ptr=&n;
The above statement assigns the address of variable n to pointer variable ptr.
Now the value of ptr is 2000.
Indirection:
The Method of accessing the value at the address held by a pointer variable is known as Indirection.
The indirection operator (*) is used to access the value at the address held by pointer variable. The * operator is also called the dereference operator.
Let understand the code snippet given.
#include<stdio.h>
int main()
{
int n= 3;
int *ptr;
ptr=&n;
printf(“\nThe value of n using pointer variable is %d”, *ptr);
return(0);
}
Output:
The value of n using pointer variable is 3
The output of the given program is 3 . we need to remember that the “ptr” is a pointer variable that hold the address of the variable “n”.
Observe from the given program that we have declared int *ptr pointer variable and
assigned the address of variable n to it.
ptr=&n;
and finally we accessed the value of n using *ptr variable called "indirection" Operator.
In other word *ptr is also known as value at address operator.
The pointer variable *ptr becomes--->*(2000) .we know from our program that the value at address 2000 is 3.
The function printf() displays the final output.
"The value of n using pointer variable is 3."
Previous Topic:-->> Union FAQ in C. || Next topic:-->>How to Use Pointer in C.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ