指针地址的混乱
我看到了这样的代码:
#include<stdio.h>
struct person{
int age;
float weight;
};
int main(){
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age); // why use &personPtr->age, shouldn't it just be personPtr->age since personPtr is itself a pointer.
printf("Enter weight: ");
scanf("%f", &personPtr->weight); // similarly here: should it be personPtr->weight instead. Why the & in front of personPtr, I thought & means address, so wouldn't it means address of the pointer which seems wrong here if we want to show the weight value.
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
// however if I do: printf("weight: %f", &personPtr->weight); this doesn't print the value of weight. So it has to do with the scanf function?
return 0;
}
我不明白的是,有什么区别: &amp; personptr-&gt;年龄和 Personptr-&Gt;年龄
上面代码中的 。当我有一个指向结构的指针并想要检索该结构的内部变量时,我总是会看到代码的年龄类型。
因此,说我有一个结构
struct A {
int x;
int y;
};
struct A f1;
struct A *f2 = &f1;
,然后我知道我可以做:
f2-&gt; x,f2-&gt; y以获取x,y变量在struct f1内的x和y的值。但是&amp; f2-&gt; x,&amp; f2-&gt; y是什么?
或使用&amp; in&amp; personptr-&gt;年龄与这里的scanf功能有关吗? (因为它在这里的scanf函数内部使用。
使用&amp; personptr- age而不是personptr- age。
有人可以解释差异,以及为什么 参见&amp;
uthread_mutex_t uthread_mutex_create () {
uthread_mutex_t mutex = malloc (sizeof (struct uthread_mutex));
mutex->holder = 0;
mutex->reader_count = 0;
spinlock_create (&mutex->spinlock); \\ why &mutex, instead of just mutex?????
uthread_initqueue (&mutex->waiter_queue);
uthread_initqueue (&mutex->reader_waiter_queue);
return mutex;
}
I see a piece of code like this:
#include<stdio.h>
struct person{
int age;
float weight;
};
int main(){
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age); // why use &personPtr->age, shouldn't it just be personPtr->age since personPtr is itself a pointer.
printf("Enter weight: ");
scanf("%f", &personPtr->weight); // similarly here: should it be personPtr->weight instead. Why the & in front of personPtr, I thought & means address, so wouldn't it means address of the pointer which seems wrong here if we want to show the weight value.
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
// however if I do: printf("weight: %f", &personPtr->weight); this doesn't print the value of weight. So it has to do with the scanf function?
return 0;
}
the thing I don't understand is what are the difference between:
&personPtr->age and
personPtr->age
in the code above. I always see peresonPtr->age type of codes when I have a pointer to a struct and want to retrieve the inner variable of that struct.
So say I have a struct
struct A {
int x;
int y;
};
struct A f1;
struct A *f2 = &f1;
then I know I can do:
f2->x, f2->y to get the value of x, and y of the x, y variables inside the struct f1. But what does &f2->x, &f2->y?
Or the use of & in &personPtr->age has to do with the scanf function here? (because its use & inside the scanf function here.
could someone explains the difference and why use the &personPtr->age instead of personPtr->age.
Another example here, for example here in the Mutex function, I again see the &
uthread_mutex_t uthread_mutex_create () {
uthread_mutex_t mutex = malloc (sizeof (struct uthread_mutex));
mutex->holder = 0;
mutex->reader_count = 0;
spinlock_create (&mutex->spinlock); \\ why &mutex, instead of just mutex?????
uthread_initqueue (&mutex->waiter_queue);
uthread_initqueue (&mutex->reader_waiter_queue);
return mutex;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
personptr
是整个对象的指针person1
,因此此表达式
personptr-&gt; age
产生数据成员 age 对象person1
您需要通过引用将其传递给scanf
,该引用是通过指针向其传递到更清楚的指针,您可以重写表达式
<代码>&amp;(personptr-&gt; age)产生数据成员的地址
age
对象的person1
。函数SCANF期望其参数是指向原始对象的指针。因此,将函数的指针删除将直接访问原始对象(而不是对象的值的副本),该对象可以以这种方式更改。还要考虑以下演示计划。
程序输出
为函数
g
通过参考(&amp; x
)接受变量x
)然后删除指针,它可以更改原始变量X
。另一方面,函数f
处理了原始变量x
的值的副本。因此,更改副本对原始变量x
没有影响。personPtr
is a pointer to the whole objectperson1
So this expression
personPtr->age
yields the data memberage
of the objectperson1
which you need to pass toscanf
by reference that is through a pointer to itTo make it more clear you may rewrite the expression like
That is the expression
&( personPtr->age )
yields the address of the data memberage
of the objectperson1
. And the function scanf expects as its argument a pointer to the original object. Thus dereferencing the pointer the function will get a direct access to the original object (instead of a copy of the value of the object) that can be changed such a way.Also consider the following demonstration program.
The program output is
As the function
g
accepts the variablex
by reference (&x
) then dereferencing the pointer it can change the original variablex
. On the other hand, the functionf
deals with a copy of the value of the original variablex
. So changing the copy has no influence on the original variablex
.在c
scanf()
要求将指针传递给它。因为personptr
是一个指向人员结构的指针,只需传递personptr-&gt; age
将提供scanf
value Person1年龄字段而不是指向它的指针,因此编译器会抱怨scanf
要求指针,但已通过整数。通过执行
&amp; personptr-&gt; age
您正在通过scanf
地址从person1
中的年龄字段的地址。因为您将其传递给了一个地址,而不是这次的整数,所以没有编译错误。&amp; f2-&gt; x
,&amp; f2-&gt; y
将分别为您提供X和Y字段的地址。相反,f2-&gt; x
,f2-&gt; y
会给您他们的 value 。同样,在您的上一个示例中,
因为
malloc
返回指针到分配的内存块的开始,mutex
只是指向非启用的指针uthread_mutex
结构。该功能
作为参数采用指针。这就是为什么您必须将其传递
&amp; mutex-&gt; spinlock
或格式不同:&amp;(mutex-&gt; spinlock)
。函数也适用于
uthread_initqueue();
anduthread_initqueue();
In C
scanf()
requieres a pointer to be passed to it. BecausepersonPtr
is a pointer to a person struct,simply passing itpersonPtr->age
would givescanf
the value of person1's age field and not a pointer to it and so the compiler would complain thatscanf
requieres a pointer but was passed an integer.By doing
&personPtr->age
you are passingscanf
the address of the age field fromperson1
. Because you passed it an address and not an integer this time, there is no compilation error.&f2->x
,&f2->y
would give you the address of the x and y fields respectively. In contrast,f2->x
,f2->y
would give you their values.Similarly in your last example,
because
malloc
returns a pointer to the start of the allocated chunk of memory,mutex
is simply a pointer to an uninitializeduthread_mutex
struct.The function
takes a pointer as it's argument. That's why you must pass it
&mutex->spinlock
or formatted differently:&(mutex->spinlock)
.The same applies for the functions
uthread_initqueue();
anduthread_initqueue();