Can someone explain how a pointer to a pointer works?

I do not understand how a pointer to a pointer works. Any way to do the same job without using a pointer to a 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);
}
+2
source share
5 answers

Pointers to pointers 101

Suppose you have a variable int x: int x;.

  • Part of the memory will be allocated for xlarge enough to accommodate int.
  • The memory assigned to xhas an address in the process memory card.
  • To see the address where it xis in memory, use:
    printf("x memory address = %p\n", (void*)&x);
  • &xmeans address x.
  • , x, :
    printf("x=%d\n", x);
  • x . , , :
    x = 42;
  • x , .
  • x ( : 42) (x), x.
  • x (&x), x.

, - p, , int: int *p;

  • p , .
  • , p, .
  • &p address of .
  • , p , :
    printf("p memory addr = %p\n", &p);
  • , p, :
    printf("Address where p :% p\n ", p);
  • , , :
    printf("int = %d\n", *p);
  • p ; p , , , .
  • . , p x:
    p = &x
  • , p, ( int p).
  • , p, ; .
  • p . , , :
    p = &x; p = NULL
  • p , .
  • p () (p), 'p', , .
  • p (&p), , p.

, - pp, , 'int': int **pp;... : void insertts (customer ** tmp):

  • .
  • ...

enter image description here

: , ?

. :

void inserts(customer **tmp);

...
   {
   customer cust;
   custPtr  custPtr = &cust;

   inserts(&custPtr);
   ... 

inserts() , , custPtr.

:

void inserts2(customer *tmp);

...
   {
   customer cust;
   custPtr  custPtr = &cust;

   inserts2(custPtr);
   ... 

insert2() custPtr, cust. , insert2() () cust, , custPtr.

+7

T, , :

void foo( T *ptr ) 
{
  *ptr = new_value();
}

void bar( void )
{
  T var;
  foo( &var ); // writes to var
}

T - Q *, :

void foo( Q **ptr )
{
  *ptr = new_value();
}

void bar( void )
{
  Q *var;
  foo( &var ); // writes to var
}

typedefs, pointerness , typedefs juju.

, , @Namfuak :

Q *foo( void )
{
  Q *val = new_value();
  return val;
}

void bar( void )
{
  Q *var;
  var = foo();
}
+4

. , .

scanf() , :

void inserts(customer **tmp)
{
    customer *new_cust = malloc(sizeof *new_cust);
    if ( !new_cust ) {
        fprintf(stderr, "Couldn't allocate memory.\n");
        exit(EXIT_FAILURE);
    }

    puts("Give me a customer name, surname code and money");
    scanf("%s %s %d %f", new_cust->name, new_cust->surname,
                         new_cust->code, new_cust->money);

    /*  We delay using multiple levels of indirection
        until we get to the very last line, here.      */

    *tmp = new_cust;
}

, , , .

+1

, , .

:

struct customer {
    char name[20];
    char surname[20];
    int code;
    float money;
};

-, float .

C FAQ 4.8. , , . ? -, , , , .. , , , .

, , -, , malloc. -, scanf . -, inserts, , .

, - create_customer_interactive, , , .

:

#include <stdio.h>
#include <stdlib.h>

struct customer {
    char *name;
    char *surname;
    int code;
    int money; /* in cents */
};

typedef struct customer *PCustomer;

int read_customer_record(FILE *fp, PCustomer customer) {
    /* dummy implementation */
    customer->name = "A. Tester";
    customer->surname = "McDonald";
    customer->code = 123;
    customer->money = 100 * 100;
    return 1;
}

PCustomer insert_record_interactive(PCustomer *db, FILE *fp) {
    PCustomer tmp = malloc(sizeof(*tmp));
    if (!tmp) {
        return NULL;
    }
    if (!read_customer_record(fp, tmp)) {
        free(tmp);
        return NULL;
    }
    *db = tmp;
    return tmp;
}

int main(void) {
    PCustomer new_customer;
    PCustomer *db = malloc(3 * sizeof(*db));
    if (!db) {
        perror("Failed to allocate room for customer records");
        exit(EXIT_FAILURE);
    }

    /* insert a record in the second slot */
    new_customer = insert_record_interactive(&db[1], stdin);
    if (!new_customer) {
        perror("Failed to read customer record");
        exit(EXIT_FAILURE);
    }

    printf(
            "%s %s (%d) : $%.2f\n",
            new_customer->name,
            new_customer->surname,
            new_customer->code,
            ((double)new_customer->money) / 100.0
            );
    return 0;
}
0
void inserts(customer *tmp[])
{
    *tmp = 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);
}

. .

, , * tmp 1 "". tmp (** tmp * tmp []) "" .

-2

Source: https://habr.com/ru/post/1543335/


All Articles