指向结构的指针

时间:2015-06-16 05:26:38

标签: c pointers struct 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

但是当我们使用指针结构

时会有一些混乱
#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;
} 

我对运营商的使用感到困惑

2 个答案:

答案 0 :(得分:5)

语法<configuration> <system.web> <customErrors mode="Off"/> </system.web> </configuration> ptr->age的缩写。

答案 1 :(得分:3)

请注意,Org.apache.haoop.hive.ql.exec.DDLTask.MetaException (message: for direct Metastore Db connections, we dont support retries at client level) ptr->age相同。这意味着您在使用(*ptr).age时获得age的值。因此,您需要ptr->age才能获取其地址。

&*ptr->age相同,*(ptr->age)*((*ptr).age)相同,后者是无效语法,编译器将发出诊断信息。您正尝试在此处取消引用某个值,因为ptr->age会给出age的值,并且无法取消引用它。