Monday, 4 July 2022

Generic Pointer in C

Void Pointer or Generic Pointer (void *) in C


Pointers: 


-> A pointer is a derived data type in C-Programming.
-> It builds from one of the fundamental data types (int, float, char, and double).
-> The declaration of a pointer variable is as follows: data_type *pointer_name

Example 1:      int *P;
                         float *x;

-> Ampersand Symbol ( & ) in C-Programming represents the "address of" operator.    

Example 2:      int quantity;
                        int *P; 
                        P = &quantity;     

-> In this syntax, integer, pointer variable 'P' store the address of an integer variable quantity

Example 3:        float rate;
                           int *P;
                           P = &rate

-> The syntax given in Example 3 is wrong. Since, Compatibility and Casting problems.
-> Therefore, we cannot assign the address of floating variable rate into the integer pointer variable P.
-> The problem of Compatibility and Casting, can be solved by using Generic Pointer.

The Generic Pointer:

-> The void pointer is a generic pointer that can represent any pointer type.
-> All pointer types can be assigned to a void pointer and a void pointer can be assigned to any pointer without casting. 

Example 4:            void *vp;    (generic pointer)
                               float rate;
                               vp = &rate;

-> A pointer variable 'vp' with type void is called generic pointer.
-> The syntax in Example 4, resolve the problem of Compatibility and Casting and void pointer vp store the address of floating point variable rate.


Q.1. Why we use a generic pointer in C-Programming ?

No comments:

Post a Comment

PRINCIPAL COMPONENT ANALYSIS (PCA)

(PCA) : - Dimension reduction analysis -> It is a technique for feature extraction from a given data set. -> There are N -number...