Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example why someone should use triple-pointers in C/C++?

Tags:

c++

c

I'm searching for an example or explanation why someone should use OR not use triple-pointers in C/C++. Are there any source-code where triple-pointer arise?

Thanks in advance.

Edit: Especially i'm looking for a source-code which uses triple-pointers.

like image 896
aGer Avatar asked May 27 '15 21:05

aGer


People also ask

What is the use of triple pointer in C?

A triple-pointer is a pointer that points to a memory location where a double-pointer is being stored. The triple-pointer itself is just one pointer. Ex. int *** is a pointer, that points to the value of a double pointer, which in turn points to the value of a single pointer, which points to the value of an int.

Can we use triple pointer in C?

One can clearly see how a triple pointer (and beyond, really) would be required. In my case if I were passing an array of pointers (or array of arrays), I would use it. Evidently it would be required if you are passing into a function that is changing the size of the multi-dimensional array.

Why should we use pointers in C?

Pointers save memory space. Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location. Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well.

What is the use of pointers Why do we use it?

We use pointers because it's easier to give someone an address to your home than to give a copy of your home to everyone.


3 Answers

The best example that comes to mind is a sparse multi-level table. For instance one way to implement properties for Unicode characters might be:

prop_type ***proptable;
...
prop_type prop = proptable[c>>14][c>>7&0x7f][c&0x7f];

In this case proptable would need to have a triple-pointer type (and possibly quadruple pointer if the final resulting type is a pointer type). The reason for doing this as multiple levels rather than one flat table is that, at the first and second levels, multiple entries can point to the same subtable when the contents are all the same (e.g. huge CJK ranges).

Here's another example of a multi-level table that I implemented; I can't say I'm terribly proud of the design but given the constraints the code has to satisfy, it's one of the least-bad implementation choices:

http://git.musl-libc.org/cgit/musl/tree/src/aio/aio.c?id=56fbaa3bbe73f12af2bfbbcf2adb196e6f9fe264

like image 115
R.. GitHub STOP HELPING ICE Avatar answered Oct 22 '22 08:10

R.. GitHub STOP HELPING ICE


If you need to return an array of pointers to variable length strings via a function parameter:

int array_of_strings(int *num_strings, char ***string_data)
{
    int n = 32;
    char **pointers = malloc(n * sizeof(*pointers));
    if (pointers == 0)
        return -1;  // Failure
    char line[256];
    int i;
    for (i = 0; i < n && fgets(line, sizeof(line), stdin) != 0; i++)
    {
        size_t len = strlen(line);
        if (line[len-1] == '\n')
            line[len-1] = '\0';
        pointers[i] = strdup(line);
        if (pointers[i] == 0)
        {
            // Release already allocated resources
            for (int j = 0; j < i; j++)
                free(pointers[j]);
            free(pointers);
            return -1;  // Failure
        }
    }
    *num_strings = i;
    *string_data = pointers;
    return 0;  // Success
}

Compiled code.

like image 43
Jonathan Leffler Avatar answered Oct 22 '22 09:10

Jonathan Leffler


If you use a linked list you have to store the address of the first element of the list ( first pointer ) .

If you need to change in that list you need another pointer ( two pointer)

If you need to pass your list that you are changing in two pointers and change it in another function you need another pointer ( three pointer )...

They are a lots of examples

like image 20
cip Avatar answered Oct 22 '22 09:10

cip