Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain how pointer to pointer works?

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);
}
like image 203
Giannis Koulouris Avatar asked Mar 25 '26 05:03

Giannis Koulouris


1 Answers

Pointers to Pointers 101

Lets say you have an int variable x: int x;.

  • A portion of memory will be assigned to x large enough to hold an int.
  • The memory assigned to x has an address in the process memory map.
  • To see the address where x is located in memory, use:
    printf("x's memory address = %p\n", (void*)&x);
  • &x means address of x.
  • To see the integer value stored in 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;
  • Outside its scope of definition, the value of x can be manipulated if its memory address is known.
  • If the value of x (ie: 42) is passed to a function(x), that function cannot manipulate the value of x.
  • If the address of 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;

  • A portion of memory will be assigned to p large enough to hold a memory address.
  • The memory assigned to p has an address in the process memory map.
  • &p means address of p.
  • To see the address where p is located in memory, use:
    printf("p's memory addr = %p\n", &p);
  • To see the address where p is pointing, use:
    printf("Address where p is pointing: %p\n", p);
  • To see the alleged integer being pointed to, use:
    printf("int = %d\n", *p);
  • The value of 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.
  • The address of an object is a pointer to that object. Hence, To cause p to point to x:
    p = &x
  • Whatever p is pointing at can be referred to by the pointer type (type int for p).
  • The amount of memory assigned to 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 = NULL
  • Outside its scope of existence, the value of p can be manipulated if its' memory address is known.
  • If the value of p (an address) is passed to a function(p), that function cannot manipulate 'p' to change where it points.
  • If the address of 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) :

  • The address of a pointer is a pointer to a pointer.
  • ...

enter image description here

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.

like image 101
Mahonri Moriancumer Avatar answered Mar 27 '26 01:03

Mahonri Moriancumer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!