Thread: whats th difference between *ptr and **ptr in a signature..

1. whats th difference between *ptr and **ptr in a signature..

when i got in my function signature node *ptr it takes the pointer as a parameter.

what happens if its node **ptr

i know that node* is a type of variable in a funtion
it says "take a node type variable called ptr"

but when i do node **ptr
by my logic it says "take a node type variable called *ptr"

i was told that in the node *ptr we cant change the parameter
but int the ** we can

and i cant see how it goes in the memory
whats the difference?

2. The difference is that "take a node type variable called *ptr" makes no sense. (Edit: That is to say, * is a syntactic element, not a character that can appear in a variable name.)

node *ptr is a pointer. If you follow the pointer, you will find a node.

node **ptr is a pointer. If you follow the pointer, you will find another pointer. If you follow that pointer, you will find a node.

3. so it goes to the pointer which ptr points to.
what about the stuff about the ability to change the input parameter with **

is it possible?

4. *ptr and **ptr are both pointers, the only difference between them is that the former has one level while the latter has two levels of indirection. You can change the input parameter with either of them since *ptr as well as **ptr are both lvalues. The exception would be if they pointed to string literals.

5. what if u made ***ptr it will be a 3rd level pointer?

6. Originally Posted by lolguy
what if u made ***ptr it will be a 3rd level pointer?
yes that would be three levels of indirection; why though would you want so many levels of indirection?

7. Originally Posted by itCbitC
yes that would be three levels of indirection; why though would you want so many levels of indirection?
Because you want to set a pointer to a pointer to a new value within a function, perhaps? [I think I've used *** about three times in my life - and I've never used **** ever].

--
Mats

8. That exactly was my point; three levels of indirection used sparsely if at all.

9. One is a pointer, (*ptr) and the other is a pointer to a pointer (**ptr).
You can go on forever eg. ***************************ptr

10. yah i think 4 is like MAX for *

11. But, would you only use **ptr when dereferencing?

I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

-Dave

12. Originally Posted by mutandis
But, would you only use **ptr when dereferencing?

I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

-Dave
Well, for the obvious reason that you can't change where the first pointer points to, just like you can't change any parameter passed in to a function. If you search the board, you will find 374 examples of people trying to create a new linked list like so:
Code:
```void create_list(Node *head, Data start_data) {
}```
and wondering why they didn't have a list when they got back to main.

13. Originally Posted by mutandis
But, would you only use **ptr when dereferencing?

I ask because I *think* I've seen it used as an argument in a function prototype (do you guys call them prototypes or declarations here?)

I've confused myself with these double asterisks myself... Could anyone give an example of when you would want a pointer to a pointer? Why wouldn't you just change where the first pointer pointed to?

-Dave
Code:
```// this might be a bit 'old style' C

main( int argv, char **argc) {

.....etc....```

eg programname parameter1 parameter2 etc....eg print file1 file2 etc...

argv is the number of parameters to a program
**argc are the parameters, a pointer to an array of strings, or expressed in another
way a pointer to a pointer to a character.

Code:
```in print file1 file2
argc[0]="file1"
argc[0][1]='i'
argc[0][4]='1'

argc[1]="file2"
argc[1][0]='f'
argc[1][4]='2'

char words[2][6];
ptr=words;
strcpy(*ptr,"word1");
ptr++;
strcpy(*ptr,"word2");

If you printed
printf("&#37;s\n",words[0]);
printf("%s\n",words[1]);

it would probably say

word1
word2

or
ptr=words;
printf("%s\n",*ptr);
ptr++
printf("%s\n",*ptr);

Should do the same.```
Then again it might fall over

14. Once again, esbo shows how little esbo really knows about C.
You cannot and should not assign a 2D-array (char [x][y]) to a pointer-to-pointer (p2p, char**).
But it's quite simple.

A pointer is typically used to modify a variable that it found outside the function that modifies it (or to avoid copying of data).
So if we create a linked list, we need to store its address in a pointer, right? So then main, for example, passes in the address of the pointer we want to assign it to, so we do:

Code:
```void foo(T** ptr);

int main()
{
T* ptr;
foo(&ptr);
}

void foo(T** ptr)
{
*ptr = something;
}```
See that? If you read from the right, first you see the variable name. Then you see the first *, saying it's a pointer. And to the left of that is the type it points to.
So ptr here pointers to a T*, or in other words, it points to a pointer.

Makes sense, yes? When you think of that the * binds to the type and not the name, it all makes sense.
http://www.research.att.com/~bs/bs_faq2.html#whitespace

And esbo, main returns int, not nothing. Plus globals variables are evil. Not to mention your (probably) faulty assignment of char[2][6] to ptr (which, again, we cannot see because you insisted on making it global), whose type is probably char**.