0

I'm trying to learn pointers in c and have been trying to write samples of code for different types of situations that involve pointers that I have found in this C book

in "Examples of pointer constructs" but I can't seem to figure out how to return a pointer to a pointer to a pointer to an int from a function or how to dereference such a pointer in main.

I have been searching for a simple example all over but couldn't find one to make it clear for me. Could anyone explain this concept and how such pointers work inside functions?

I've had no problems returning a pointer to int from a function and understood the process but higher levels gave me a hard time. I've been trying to write very basic examples but I can't manage to store what this function returns in main, and also how I should dereference it:

int ***func(int x) {
    int n = x * x;
    int *p1, **p2, ***p3;
    p1 = &n;
    p2 = &p1;
    p3 = &p2;

    return &p3;
}
3
  • 1
    write some code and then share code if you don't get desired output Commented Jul 27, 2018 at 12:01
  • 1
    and read about three starts programmers Commented Jul 27, 2018 at 12:04
  • if you try to learn pointers, focus on the * and ** and forget the higher levels of indirection Commented Jul 27, 2018 at 12:05

1 Answer 1

5

You have one more level of indirection at the end than you need:

return &p3;

p3 has type int ***, which matches the return value of the function. However you're returning the address of p3 which has type int ****. That's a type mismatch. You can correct this by just returning p3:

return p3;

Assuming for a moment that the pointer you return is valid (as well as all intermediate pointers), the you would assign the return value of the function to a variable of type int ***. Then you would have to dereference 3 times to get the actual integer value:

int ***rval = func(2);
printf("value=%d\n", ***rval);

There's a bigger problem here however, and that's that you're not returning valid pointers. The pointer value you're returning contains the address of a local variable (i.e. the address of p2). When the function returns that variable goes out of scope and thus its address is invalid. Attempting to dereference a pointer to an out of scope variable is undefined behavior.

For the memory you're pointing to to be valid after the function returns, you would need to dynamically allocate memory using malloc.

Also, it's very rare that you'd actually need a triple pointer (i.e. int ***). If you find yourself using a variable of this type, that's a good sign that you need to rethink your design.

Sign up to request clarification or add additional context in comments.

3 Comments

Managed to do it using malloc . I agree that a triple pointer is rare, but I'm just trying to understand how pointers and memory work in general, I'm not actually using it in a program. Thanks.
@Isabelle Glad I could help. Feel free to accept this answer if you found it useful.
Even the initialization to rval from func(2); with a out of scope pointer is UB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.