Pointer declaration in C language.

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.


3. pointers to structures

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);
}

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.