Pat*_*tel -1 c struct pointers operators
声明为原子数据类型(如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
Run Code Online (Sandbox Code Playgroud)
但是当我们使用指向结构的指针时会有一些混乱
#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;
}
Run Code Online (Sandbox Code Playgroud)
我对这里运算符的使用感到困惑