• C

what is the meaning of pointer to pointer ?

i am new to c ' and am confused by the concept pointers to pointers. Would be
grateful if someone could advise me when and for which situations this is needed and how the concept is applied,

thanks
mallocman
mallocmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexoCommented:
A pointer points to something, might as well point to another pointer.

Let's say you have a function that reads some text from a file and returns a pointer to it.

    #include <stdio.h>
    #include <malloc.h>

    char* f(char* filename, int length)
    {
        FILE* f;
        char* p;
       
        f = fopen(filename, "r");
        p = calloc(length + 1, sizeof(char));
        fread(p, sizeof(char), length, f);
        fclose(f);
        return p;
    }

Then you use it:

    void main()
    {
        char* p;
        p = f("c:/autoexec.bat", 20);
        /* do something with p */
        free(p);
    }

Oh, but there's a little problem.  What happens if you supply a non-existant file name?  Or there is some I/O problem?  Or there is not enough memory to allocate the buffer?

Your function better return a success/failure code.

But wait!  How can a function return both a success/failure code *and* a pointer to text?  There is only one return value...

The solution is: you return success/failure code and you pass a pointer to a value that the function may change.

That is, something like:

    void change_me(int* x)
    {
        *x = 3;
    }

When called as:

    int x = 5;
    change_me(&x);

The value of x will change to 3.

Back to the original example, you have a return code and a pointer to an argument that will hold the pointer-to-text (here you have it: pointer to pointer).

    #include <stdio.h>
    #include <malloc.h>

    int f(char* filename, int length, char** pointer)
    {
        FILE* f;
       
        f = fopen(filename, "r");
        if (f == NULL)
            return 0;

        *pointer = calloc(length + 1, sizeof(char));
        if (*pointer == NULL)
            return 0;
       
        fread(*pointer, sizeof(char), length, f);
        if (ferror(f))
        {
            free(*pointer);
            return 0;
        }

        fclose(f);
        return 1;
    }

    void main()
    {
        char* p;
        int code;

        code = f("c:/autoexec.bat", 20, &p);
        if (code)
        {
            /* do something with p */
            free(p);
        }
    }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aperdonCommented:
Pointers are generally used for dynamic memory, like in malloc.
Also pointers are handy when calling other functions. The other function then is able to change the contents of the parameter. Pointers are also used in case whe parameters are big in size (for example 200 bytes or so). In that case u call the function with a pointer which is 4 bytes.
0
alexoCommented:
mallocman?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

mallocmanAuthor Commented:
thanks, can you explain it more easily ?
0
alexoCommented:
A pointer holds an address of a variable.
The variable may be an integer, a floating point or even another pointer.

Pointers are commonly used to pass arguments that may be changed by the function.  For example:

    void (int* x)
    {
        *x = 3;
    }

If you want to pass a pointer to a function *and* allow the function to change it, use a pointer to pointer.

Another use is for dynamically-allocated multidimentional arrays.  Quote:

        How can I dynamically allocate a multidimensional array?

        The traditional solution is to allocate an array of pointers,
        and then initialize each pointer to a dynamically-allocated
        "row."  Here is a two-dimensional example:

                #include <stdlib.h>

                int **array1 = malloc(nrows * sizeof(int *));
                for(i = 0; i < nrows; i++)
                        array1[i] = malloc(ncolumns * sizeof(int));

        (In real code, of course, all of malloc's return values would
        be checked.)

        You can keep the array's contents contiguous, at the cost of
        making later reallocation of individual rows more difficult,
        with a bit of explicit pointer arithmetic:

                int **array2 = malloc(nrows * sizeof(int *));
                array2[0] = malloc(nrows * ncolumns * sizeof(int));
                for(i = 1; i < nrows; i++)
                        array2[i] = array2[0] + i * ncolumns;

        In either case, the elements of the dynamic array can be
        accessed with normal-looking array subscripts: arrayx[i][j]
        (for 0 <= i < nrows and 0 <= j < ncolumns).

        If the double indirection implied by the above schemes is for
        some reason unacceptable, you can simulate a two-dimensional
        array with a single, dynamically-allocated one-dimensional
        array:

                int *array3 = malloc(nrows * ncolumns * sizeof(int));

        However, you must now perform subscript calculations manually,
        accessing the i,jth element with array3[i * ncolumns + j].  (A
        macro could hide the explicit calculation, but invoking it would
        require parentheses and commas which wouldn't look exactly like
        multidimensional array syntax, and the macro would need access
        to at least one of the dimensions, as well.  See also question
        6.19.)

0
mallocmanAuthor Commented:
alexo, thank you for your help
0
alexoCommented:
have fun!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.