2

Why would you do this:

void f(Struct** struct)
{
   ...
}

If I wish to operate on a list of structs, is it not enough to pass in a Struct*? This way I can do struct++ to address the next struct or am I very confused here? :)

Wouldn't it only be useful if I want to rearrange the list of structs in some way? However if I'm just reading I don't see the point.

5 Answers 5

2

It depends on what your data structure looks like. Assuming that p is a null-terminated array of pointers to struct s, you can run through it using a loop like this:

void f(struct s **p)
{
    while (*p != NULL) {
        /* some stuff */
        (*p)++;
    }
}

Generally, use a pointer to a pointer is useful only if you attempt to modify the pointer itself.

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

8 Comments

this works only under the assumption that the structs are placed directly behind each other in memory
@Mogria Or if the structs are nodes in a linked list, only the loop would be different.
yeah, sorry i down voted because this is not safe in most contexts and makes a ton of assumptions. this is assuming that the double pointer is not a list of pointers to individually allocated instances - a reasonably common occurance - but a pointer to a pointer to a list of objects, which as you say is only useful if you want the caller to get a modified value of *p back.
@jheriko: As far I can see, the OP doesn't specify what it was, so it is not completely wrong.
sure, but its not made clear that the answer makes these assumptions - just the comment at the end about only working if you want to modify the pointer - which is evidence of the assumption rather than statement of it
|
1

If you want to modify the pointer in caller which was passed to this function, you'd typically do this.

Because, everything is passed by value in C, passing struct* will only pass the copy of the pointer and won't modify pointer in the caller. Why passing struct * is explained in this C-FAQ.

If you don't intend to modify the pointer in caller, it's not neccessary to pass struct **.

Comments

1

There are a number of uses for this kind of parameter...

One already mentioned, and quite common is to allow the caller to use the function to modify a pointer. The obvious case here would be when getting some blob of data...

void getData( void** pData, int* size )
{
    *pData = getMyDataPointer();
    *size = getMyDataSize();
}

Another option is that perhaps the extra level of indirection allows for the list to behave in some way? e.g. by using indices to refer to specific elements they can be allocated and reallocated without having the risk of dangling pointers.

Yet another option is that the list is very large and lives in fragmented memory, or is rapidly accessed so that the list is actually several smaller lists grouped together. This sort of technique can also be used to 'lazily' allocate huge arrays, e.g. providing an interface to an array of a billion elements, but then allocating chunks of 100k on demand as they are read/written with struct** pointing at the whole thing, and each struct* being either null or pointing to 100k structs...

To be honest the context is quite important... there are plenty of uses for also triple pointers as function parameters that follow similar reasoning. (e.g. combine the first thing i mention with the second, or the second with the third etc.)

Comments

0

You are correct, there is no reason to pass a pointer to pointer unless your function is intended to modify the pointer passed in. In case of accessing an array of structs, a single level of indirection is definitely sufficient.

2 Comments

i disagree. it is important to understand the intent of the data structure before assuming this... this is one possible answer and the statement that there 'is no reason' is plainly wrong.
@jheriko There's no "assumption" here: the OP clearly states that "I wish to operate on a list of structs", so the intent is perfectly clear. Had it been a list of lists, my answer would be different.
0

The creator of the API probably thought that the argument list would be easier to memorize if the first argument of every function is the same.

Comments

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.