Help with Programming in C - Pointers...

chiwawa626

Lifer
Aug 15, 2000
12,013
0
0
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 :confused:
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
remember all pointers are, are memory addresses, two '*'s just means its a pointer to a pointer
 

MAME

Banned
Sep 19, 2003
9,281
1
0
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
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
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.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
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.
 

sandmanwake

Golden Member
Feb 29, 2000
1,494
0
0
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?
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
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.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
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)