• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with Programming in C - Pointers...

Suppose you had a function like this:

void delete(Stack ** stackpp) {

//do stuff

}

And you had the variable:
Stack * the_stack = 0;

How would you call that function, what exactly do you pass into it?
delete(???);

In the body of the function delete, suppose you wanted to free the memory that stackpp is pointing to, and you call free(address); what would u put in the parenthesis.

Pointers are confusing me 😕
 
I think you need it to be free(&stackpp) or maybe it's (*stackpp)....I think & though

dunno for sure though...I haven't used free before
 
Originally posted by: chiwawa626
And if i wanted to free that address would i call free(stackpp); like that?

You would do free(*stackpp). I believe that stackpp is a pointer to a pointer in the Delete function because you need free the memory and then set the pointer to null. The only way you could set the pointer to null is if it were passed by reference thus a pointer to a pointer. That's how we did it in my data structs class.
 
ok something's weird with your delete() function. what is it supposed to do?

because the way it is now, you can only pass &the_stack to it. the_stack, *the_stack etc would all be the wrong type. passing &the_stack is kind of strange, you woudln't want to do that unless you want to change the value of the_stack or to free the pointer itself which in this case would be wrong since the_stack pointer is not dyanmically allocated.
 
Ok, since there's a thread already about pointers, I have one also that the OP's post and Amesh's reply got me curious about. I don't use pointers too often except when I'm passing by reference, but what's the point of a double pointer?

For instance, if you're passing by value, you would not use pointers and just pass a copy of that value into the function. If passing by reference, when you're wanting the function to update the value of the original variable, you would use a pointer and pass the address of the variable into the function. In what instance is a double pointer used?
 
The general case is simply if you need to modify a pointer. void doSomething(int *num) is able to modify the integer pointed to by num, whereas void doSomething(int **num) would change num to point to a different integer.

Imagine that you wanted to write a function to create an integer array of size n. The code could look like this:

void main()
{
int *x = NULL;
createArray(&x,10);
}

void createArray(int **num, size n)
{
*num = (int*)malloc(sizeof(int) * n);
}

You would also use double pointers if you needed to operate on a multidimensional array, and there are plenty of other specific uses for them.
 
Originally posted by: sandmanwake
Ok, since there's a thread already about pointers, I have one also that the OP's post and Amesh's reply got me curious about. I don't use pointers too often except when I'm passing by reference, but what's the point of a double pointer?

For instance, if you're passing by value, you would not use pointers and just pass a copy of that value into the function. If passing by reference, when you're wanting the function to update the value of the original variable, you would use a pointer and pass the address of the variable into the function. In what instance is a double pointer used?

if you wanted to change what the pointer points too you need to pass a double pointer,


in cases like the one above the reason why its stack ** is most likely the function deletes the object and sets the pointer to null. (which is a good habit to get into)
 
Back
Top