I don't really understand how the pointer to pointer works. Any way to do the same work without using pointer to pointer?
struct customer
{
char name[20];
char surname[20];
int code;
float money;
};
typedef struct customer customer;
void inserts(customer **tmp)
{
*tmp = (customer *)malloc(sizeof(customer));
puts("Give me a customer name, surname code and money");
scanf("%s %s %d %f", (*tmp)->name, (*tmp)->surname, &(*tmp)->code, &(*tmp)->money);
}
Pointers to Pointers 101
Lets say you have an int variable x: int x;.
x large enough to hold an int.x has an address in the process memory map.x is located in memory, use: printf("x's memory address = %p\n", (void*)&x);&x means address of x.x, use: printf("x=%d\n", x);x can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared: x = 42;x can be manipulated if its memory address is known.x (ie: 42) is passed to a function(x), that function cannot manipulate the value of x.x is passed to a function(&x), that function can manipulate the value of x.Now, lets say that you have a pointer variable p that assumes it points to an int: int *p;
p large enough to hold a memory address.p has an address in the process memory map.&p means address of p.p is located in memory, use: printf("p's memory addr = %p\n", &p);p is pointing, use: printf("Address where p is pointing: %p\n", p);printf("int = %d\n", *p);p is an address in memory; and p can be set to point to any address, whether or not that address actually exists in the process memory map. p to point to x:p = &xp is pointing at can be referred to by the pointer type (type int for p).p will vary, depending on the architecture of the machine; and how it represents an address.p can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared: p = &x; or p = NULLp can be manipulated if its' memory address is known.p (an address) is passed to a function(p), that function cannot manipulate 'p' to change where it points.p is passed to a function(&p), that function can manipulate what p points to.Now, lets say that you have a pointer to a pointer variable pp that assumes it points to a pointer to an 'int': int **pp; ...or: void inserts(customer **tmp) :

Back to the question
Q: Any way to do the same work without using pointer to pointer?
No. Assume the following:
void inserts(customer **tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts(&custPtr);
...
The inserts() function requires the address of a pointer in order to manipulate where custPtr points.
If instead:
void inserts2(customer *tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts2(custPtr);
...
The insert2() would get a copy of the value of custPtr, which is the address of cust. Hence, insert2() could modify the value(s) of cust, but could not change where custPtr is pointing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With