acc*_*511 2 c malloc struct pointers
我真的不明白指针指针是如何工作的.没有使用指针指针的任何方式做同样的工作?
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);
}
Run Code Online (Sandbox Code Playgroud)
指针指针101
让我们说你有一个int变量x:int x;.
xint
xx),则该函数不能操作`x`的值.printf("x's memory address = %p\n", (void*)&x);),该函数可以操作`x`的值.现在,假设您有一个指针变量&x,假定它指向x:x
printf("x=%d\n", x);xx = 42;xx或xx),则该函数不能操纵'p'来改变它指向的位置.x),那么该函数可以操作`p`指向的内容.现在,假设你有一个指向指针变量的指针&x,它指向一个指向'int'的指针:x ...或:void inserts(customer **tmp):

回到问题
问:没有使用指向指针的方法可以做同样的工作吗?
不.假设如下:
void inserts(customer **tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts(&custPtr);
...
Run Code Online (Sandbox Code Playgroud)
该p函数需要指针的地址才能操纵int点的位置.
如果相反:
void inserts2(customer *tmp);
...
{
customer cust;
custPtr custPtr = &cust;
inserts2(custPtr);
...
Run Code Online (Sandbox Code Playgroud)
在int *p;将得到的值的副本p,这是地址p.因此,&p可以修改值address of,但不能改变p指向的位置.