Pointer to Pointer β A simple pointer to another pointer Commonly used in various scenarios where indirection or multiple levels of indirection are required.
Example:
char *ptr =(char*)malloc(sizeof(char));
char **ptr_to_ptr =&ptr;
Pointer to Arrays β A point to an array. Can be used for manipulating the array or collecting an array from a function.
Example:
char array[] ={ βwβ,βoβ,βrβ,βkβ,βsβ};
char *ptr_to_array = array;
Pointer to multidimensional arrays β A pointer to collect a multidimensional array. Can be used to traversal and manipulation of multidimensional array values.
Example:
int multi_arr[][4] ={ {10,19,20,12}, {30,59,32,77}, {11,2578,15,117} };
int (*ptr_to_multi_arr)[4] = multi_arr;
*(ptr_to_multi_arr) gives first array
*(*(ptr_to_multi_arr) + 1) gives first array second element
Pointer to functions: Β Can be uses to pass function pointers as arguments. Ex: Can be used in file operations, system calls, operations on device drivers.
Example 1:
loff_t (*llseek) (struct file *, loof_t, int);
ssize_t (*read) (struct file *, char __user *,size_t, loof_t*);
Example 2:
int fun1(int a, int b) { // Operation1β¦}
int fun2(int c, int d) { // Operation 2β¦}
void main()
{
int some_val1,some_val2,some_val3;
int store_some_val;
int (*ptr_to_function)(int,int);
ptr_to_function = fun1;
store_some_val = ptr_to_function( some_val1, some_val2);
ptr_to_function = fun2;
store_some_val = ptr_to_function( some_val3, some_val1);
}
Array of structures β Used to store multiple strcutures in continuous block of memory. Can be useful when working on multiple instances of structure and perform operations as a whole. Ex: Process Control Blocks. Scull_dev strcut to avoid race conditions.
Example:
struct scull_dev{
β¦.
int quantum;
int qset;
unsigned long size; β¦
struct semaphore sem;
β¦.
};
struct scull_dev scull_devices[10];
Pointers to structures: Used to dynamically allcoate memory for structires and access its contents indirectly. Useful to modify the structure the structure member contents without making a copy.
Example:
struct scull_pipe{
β¦
char *rp ,*wp;
int nreaders, nwriters;
β¦
struct cdev cdev; // Char driver structure
}
static ssize_t scull_p_read( struct file *filp, char __user *bud, size_t count, loff_t *f_pos)
{
Struct scull_pipe *dev = filp->private_data; // dev is a pointer to scull_pipe structure used to store private_data from filp structure.
β¦
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
