In this tutorial we will learn different ways of declaring pointer in C Language.
What is pointer declaration in C with example?
1. Pointer declaration:
Pointer declaration is the process to declare pointer variable but do not initialize it. * dereference operator is used before the name of pointer variable at the time of declaration.
syntax:
datatype * ptr;
e.g
int *ptr;
where
• datatype is the type of data that pointer is pointing to.
• * is the dereferencing operator in the pointer declaration.
• ptr is the name of the pointer variable.
• Pointer declared will points to some random memory address.
In the example given above the pointer *ptr declared and will points to some random memory address as it is not initialized.
Pointer declaration in C programming.
2.Pointer to an Array | Array Pointer declaration:
In the c programming the array pointer is the another name to a pointer to an array. This type of array generally used to access the various components of any given array.
Syntax:
data_type (*variable_name)[size_of_array];
Here:
• data_type is the type of data of the elements that the array stores.
• variable_name is the name given to the pointer variable.
• size_of_array is the size of the array.
Example
float (*ptr)[5];
Here ptr is pointer that can point to an array of 10 floating point values. As we know that subscript [ ] have higher precedence than indirection,. One thing we note here is that we need to enclose the indirection operator and pointer name inside parentheses. Here the type of ptr is ‘pointer to an array of 5 floats.
#include<stdio.h>
int main()
{
float salary[5] = { 1568.43, 32444.67, 4334.87, 45567.90, 23598.90 };
int *ptr = salary;
printf("%p\n", ptr);
return 0;
}
ptr is the pointer variable In the above program that points to the first element of an array i.e. 0th element of the array. A pointer that points to whole array can also be declare in c language.
Pointer to structure provide a powerful way of handling complex data structure effectively. Pointer to structure holds the address of the entire structure.
Pointer to structure is s used to create c data structures such as linked lists, graphs,trees, etc.
Syntax:
struct tagname *ptr;
For example − struct employee *e ;
Accessing
Given following is the syntax to access pointers to structures.
Ptr-> membername;
For example :−
e->empno, e->ename, e->salary;
The C program to show the usage of pointers to structures
#include<stdio.h>
struct employee{
int empno;
char ename[30];
float salary;
};
int main ( ){
struct employee e;
struct employee *emp;
printf("\nEnter sno, sname, marks:");
scanf ("%d%s%f", & e.empno, e.ename, &e. salary);
emp = &e;
printf ("\nDetails of the employee are");
printf ("\nNumber = %d
", emp ->empno);
printf ("\nEmployee name = %s
", emp->ename);
printf ("\nSalary =%f
", emp ->salary);
return(0);
}
#include<stdio.h>
struct employee{
int empno;
char ename[30];
float salary;
};
int main ( ){
struct employee e;
struct employee *emp;
printf("\nEnter sno, sname, marks:");
scanf ("%d%s%f", & e.empno, e.ename, &e. salary);
emp = &e;
printf ("\nDetails of the employee are");
printf ("\nNumber = %d
", emp ->empno);
printf ("\nEmployee name = %s
", emp->ename);
printf ("\nSalary =%f
", emp ->salary);
return(0);
}
Output:
enter empno, ename, salary:
7900 John 9800
Details of the employee are:
Number = 7900
Employee Name = 9800
marks =9800.000000
4. Pointer to function.
To understand pointer to function well, let us first have a look.
how to declare a function?
float show(float);
Here show is a function that returns float and takes one argument of float type. So as a logical guy will think, putting a * operator between float and show (float) should create a pointer to a function i.e.
float * show (float);
But Oops..C operator precedence also plays a role here ..so in this case, operator () will take priority over operator *.
And the above declaration will mean – a function show with one argument of float type and return value of float * i.e. integer pointer. So it did something that we didn’t want to do.
So as a next logical step, we have to bind operator * with show somehow. And for this, we would change the default precedence of C operators using () operator.
Example in C to understand how to declare a pointer to a function.
// C Program to illustrates how to declare a function
// pointer to add two floating point number
#include <stdio.h>
float add(float x, float y)
{
return x + y;
}
int main()
{
// Assigning function address using & operator
float (*add_ptr)(float, float) = &add;
// Calling the function using the function pointer
float result = add_ptr(30.2, 24.0);
printf("Result: %.2f\n", result);
return 0;
}
Output
Result: 54.20
Previous Topic:-->> How to use Pointers in C. || Next topic:-->>Initialization of 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