指针地址的混乱

发布于 2025-02-09 00:20:18 字数 1875 浏览 3 评论 0原文

我看到了这样的代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2025-02-16 00:20:18

personptr是整个对象的指针person1

personPtr = &person1;

因此此表达式personptr-&gt; age产生数据成员 age 对象person1您需要通过引用将其传递给scanf,该引用是通过指针向其

scanf("%d", &personPtr->age);

传递到更清楚的指针,您可以重写表达式

scanf("%d", &( personPtr->age ));

<代码>&amp;(personptr-&gt; age)产生数据成员的地址age对象的person1。函数SCANF期望其参数是指向原始对象的指针。因此,将函数的指针删除将直接访问原始对象(而不是对象的值的副本),该对象可以以这种方式更改。

还要考虑以下演示计划。

#include <stdio.h>

void f( int x )
{
    x = 10;
}

void g( int *px )
{
    *px = 10;
}

int main( void )
{
    int x = 0;

    printf( "Before calling f() x = %d\n", x );

    f( x );

    printf( "After  calling f() x = %d\n", x );

    printf( "Before calling g() x = %d\n", x );

    g( &x );

    printf( "After  calling g() x = %d\n", x );
}

程序输出

Before calling f() x = 0
After  calling f() x = 0
Before calling g() x = 0
After  calling g() x = 10

为函数g通过参考(&amp; x)接受变量x)然后删除指针,它可以更改原始变量X。另一方面,函数f处理了原始变量x的值的副本。因此,更改副本对原始变量x没有影响。

personPtr is a pointer to the whole object person1

personPtr = &person1;

So this expression personPtr->age yields the data member age of the object person1 which you need to pass to scanf by reference that is through a pointer to it

scanf("%d", &personPtr->age);

To make it more clear you may rewrite the expression like

scanf("%d", &( personPtr->age ));

That is the expression &( personPtr->age ) yields the address of the data member age of the object person1. 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.

#include <stdio.h>

void f( int x )
{
    x = 10;
}

void g( int *px )
{
    *px = 10;
}

int main( void )
{
    int x = 0;

    printf( "Before calling f() x = %d\n", x );

    f( x );

    printf( "After  calling f() x = %d\n", x );

    printf( "Before calling g() x = %d\n", x );

    g( &x );

    printf( "After  calling g() x = %d\n", x );
}

The program output is

Before calling f() x = 0
After  calling f() x = 0
Before calling g() x = 0
After  calling g() x = 10

As the function g accepts the variable x by reference (&x) then dereferencing the pointer it can change the original variable x. On the other hand, the function f deals with a copy of the value of the original variable x. So changing the copy has no influence on the original variable x.

山川志 2025-02-16 00:20:18

在c scanf()要求将指针传递给它。因为personptr是一个指向人员结构的指针,只需传递personptr-&gt; age将提供scanf value Person1年龄字段而不是指向它的指针,因此编译器会抱怨scanf要求指针,但已通过整数。

通过执行&amp; personptr-&gt; age您正在通过scanf 地址person1中的年龄字段的地址。因为您将其传递给了一个地址,而不是这次的整数,所以没有编译错误。

然后我知道我可以做:

f2-&gt; x,f2-&gt; y以获取x的值
结构F1。但是&amp; f2-&gt; x,&amp; f2-&gt; y?

&amp; f2-&gt; x&amp; f2-&gt; y将分别为您提供X和Y字段的地址。相反,f2-&gt; xf2-&gt; y会给您他们的 value

同样,在您的上一个示例中,

uthread_mutex_t mutex = malloc (sizeof (struct uthread_mutex));

因为malloc返回指针到分配的内存块的开始,mutex只是指向非启用的指针uthread_mutex结构。

该功能

spinlock_create(&mutex->spinlock);

作为参数采用指针。这就是为什么您必须将其传递&amp; mutex-&gt; spinlock或格式不同:&amp;(mutex-&gt; spinlock)

函数也适用于uthread_initqueue(); and uthread_initqueue();

In C scanf() requieres a pointer to be passed to it. Because personPtr is a pointer to a person struct,simply passing it personPtr->age would give scanf the value of person1's age field and not a pointer to it and so the compiler would complain that scanf requieres a pointer but was passed an integer.

By doing &personPtr->age you are passing scanf the address of the age field from person1. Because you passed it an address and not an integer this time, there is no compilation error.

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?

&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,

uthread_mutex_t mutex = malloc (sizeof (struct uthread_mutex));

because malloc returns a pointer to the start of the allocated chunk of memory, mutex is simply a pointer to an uninitialized uthread_mutex struct.

The function

spinlock_create(&mutex->spinlock);

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(); and uthread_initqueue();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文