Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use double indirection? or Why use pointers to pointers?

Tags:

c

pointers

When should a double indirection be used in C? Can anyone explain with a example?

What I know is that a double indirection is a pointer to a pointer. Why would I need a pointer to a pointer?

like image 888
manju Avatar asked Apr 07 '11 12:04

manju


People also ask

Why do we use pointers to pointers?

A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer.

Why do we need double pointer?

Double pointers can also be used when we want to alter or change the value of the pointer. In general double pointers are used if we want to store or reserve the memory allocation or assignment even outside of a function call we can do it using double pointer by just passing these functions with ** arg.

What is double indirection?

The most common form of indirection is the act of manipulating a value through its memory address. For example, accessing a variable through the use of a pointer. A stored pointer that exists to provide a reference to an object by double indirection is called an indirection node.


1 Answers

If you want to have a list of characters (a word), you can use char *word

If you want a list of words (a sentence), you can use char **sentence

If you want a list of sentences (a monologue), you can use char ***monologue

If you want a list of monologues (a biography), you can use char ****biography

If you want a list of biographies (a bio-library), you can use char *****biolibrary

If you want a list of bio-libraries (a ??lol), you can use char ******lol

... ...

yes, I know these might not be the best data structures


Usage example with a very very very boring lol

#include <stdio.h> #include <stdlib.h> #include <string.h>  int wordsinsentence(char **x) {     int w = 0;     while (*x) {         w += 1;         x++;     }     return w; }  int wordsinmono(char ***x) {     int w = 0;     while (*x) {         w += wordsinsentence(*x);         x++;     }     return w; }  int wordsinbio(char ****x) {     int w = 0;     while (*x) {         w += wordsinmono(*x);         x++;     }     return w; }  int wordsinlib(char *****x) {     int w = 0;     while (*x) {         w += wordsinbio(*x);         x++;     }     return w; }  int wordsinlol(char ******x) {     int w = 0;     while (*x) {         w += wordsinlib(*x);         x++;     }     return w; }  int main(void) {     char *word;     char **sentence;     char ***monologue;     char ****biography;     char *****biolibrary;     char ******lol;      //fill data structure     word = malloc(4 * sizeof *word); // assume it worked     strcpy(word, "foo");      sentence = malloc(4 * sizeof *sentence); // assume it worked     sentence[0] = word;     sentence[1] = word;     sentence[2] = word;     sentence[3] = NULL;      monologue = malloc(4 * sizeof *monologue); // assume it worked     monologue[0] = sentence;     monologue[1] = sentence;     monologue[2] = sentence;     monologue[3] = NULL;      biography = malloc(4 * sizeof *biography); // assume it worked     biography[0] = monologue;     biography[1] = monologue;     biography[2] = monologue;     biography[3] = NULL;      biolibrary = malloc(4 * sizeof *biolibrary); // assume it worked     biolibrary[0] = biography;     biolibrary[1] = biography;     biolibrary[2] = biography;     biolibrary[3] = NULL;      lol = malloc(4 * sizeof *lol); // assume it worked     lol[0] = biolibrary;     lol[1] = biolibrary;     lol[2] = biolibrary;     lol[3] = NULL;      printf("total words in my lol: %d\n", wordsinlol(lol));      free(lol);     free(biolibrary);     free(biography);     free(monologue);     free(sentence);     free(word); } 

Output:

total words in my lol: 243
like image 84
pmg Avatar answered Sep 23 '22 01:09

pmg