gpt4 book ai didi

c - 指向结构体的指针

转载 作者:行者123 更新时间:2023-11-30 18:18:43 25 4
gpt4 key购买 nike

声明为原子数据类型(例如 int)时,指针的工作方式如下

int a,*b=&a;

printf("\n The address of pointer b = %p",&b); //Here using & operator we get the memory location where the pointer b is stored itself

printf("\n The pointer b points to %p this memory location",b); //The will give me the value of ptr and that value is the memory address of the variable a

printf("\n The value at the memory where pointer b points is %d",*b);//Using * operator we get the value stored at the memory location hold by b

但是当我们使用结构体指针时会出现一些困惑

#include<stdio.h>
struct A{
int age;
int roll_no;
float marks;
};
int main(void)
{
struct A obj1;
struct A *ptr;
printf("\n The addrees of the obj1 is =%p",&obj1);
printf("\n The address of the variable age is %p ",&obj1.age);

ptr=&obj1;
printf("\n THe pointer ptr points to %p ",ptr); //This will give me the memory location where pointer ptr is pointing to.

printf("\n The memory address of pointer ptr itself is %p ",&ptr); //This will give the memory location where the pointer ptr is itself store. So far So good

printf("\n The memory location of variable age is %p",&ptr->age); //Why I have to use this & operator to find the address of the age and we also do not use * opertaor here I guess

/* Should not ptr->age give me the memory address and *ptr->age give me the value ? */


return 0;
}

我对这里运算符的使用感到困惑

最佳答案

语法ptr->age(*ptr).age的缩写。

关于c - 指向结构体的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com