简体   繁体   English

如何定义结构指针和非指针(. 和 ->)?

[英]How do I define struct pointers and non-pointers (. and ->)?

If my struct looks like:如果我的结构看起来像:

struct contact {
    char* name;
    int number;
};

and one of the functions I'm writing looks like:我正在编写的其中一个函数如下所示:

int find(struct contact* pb, int len, char* name)
{
    for (int i = 0; i < len; i++)
        if (strcmp(name, pb->name[i]) == 0) // pb[i].name is OK!
            return pb[i].number; // pb->number[i] ? - but number is not a pointer
.
.
.

If we will take a closer look at the function find , we can see that the following: pb->name[i] is like writing pb[i].name is the same.如果我们仔细看看 function find ,我们可以看到以下内容: pb->name[i]就像写pb[i].name是一样的。 But what about: pb[i].number ?但是: pb[i].number呢? I know that number is not a pointer, so how can I write it similair to: pb[i].number ?我知道这个number不是一个指针,所以我怎么能把它写成: pb[i].number It can not be: pb->number[i] , right?不能是: pb->number[i] ,对吗?

pb->name[i] is not the same as pb[i].name . pb->name[i]pb[i].name不同。

pb->name[i] tells the computer to: pb->name[i]告诉计算机:

  • Look at the value stored in the variable pb (which is an address)查看存储在变量pb中的值(这是一个地址)
  • Look at the struct which that address points to ( -> )查看该地址指向的结构( ->
  • Look at the name in that struct.查看该结构中的name
  • Look at the i -th thing in the array which name points to.查看name指向的数组中的第i个事物。

ie it returns the i -th character in the first contact's name.即它返回第一个联系人姓名中的第i个字符。

pb[i].name tells the computer to: pb[i].name告诉计算机:

  • Look at the value stored in the variable pb (which is an address)查看存储在变量pb中的值(这是一个地址)
  • Look at the i -th struct in the array which that address points to查看该地址指向的数组中的第i个结构
  • Look at the name in that struct.查看该结构中的name

ie it returns the address of the i -th contact's name.即它返回第i个联系人姓名的地址。

pb->number[i] makes no sense because number isn't an array. pb->number[i]没有意义,因为number不是数组。 You're asking for the i -th thing from the first contact's number, but the number isn't an array or a pointer to one.您从第一个联系人的号码中要求第i个东西,但该号码不是数组或指向一个的指针。

pb->name[i] is also wrong but it makes sense. pb->name[i]也是错误的,但它是有道理的。 You're asking the computer for something which isn't the thing you want, but at least it's a thing which is possible, so the computer does it anyway.你在向计算机索要一些你不想要的东西,但至少它是可能的,所以无论如何计算机都会这样做。

The array subscript operation a[i] is exactly equivalent to *(a + i) - given a starting address a , compute the address of the i 'th object following that address and dereference the result.数组下标操作a[i]完全等同*(a + i) - 给定起始地址a ,计算该地址后第iobject的地址并取消引用结果。

This means that the expression *pb is equivalent to *(pb + 0) , which is equivalent to pb[0] .这意味着表达式*pb等价于*(pb + 0) ,它等价于pb[0] The subscript operation implicitly dereferences pb , so the type of the expression pb[i] is struct contact .下标操作隐式取消引用pb ,因此表达式pb[i]的类型是struct contact

You use the .您使用. operator when the operand is a struct or union type, and the -> operator when the operand is a pointer to a struct or union type.操作数是structunion类型时的运算符, ->操作数是指向structunion类型的指针时的运算符。

Since the expression pb[i] has type struct contact , you'd use the .由于表达式pb[i]的类型为struct contact ,因此您将使用. operator to access both the name and number members:操作员访问namenumber成员:

if ( !strcmp( name, pb[i].name ) ) 
  return pb[i].number;

pb->name[i] is not the same as pb[i].name , and as used in your code will lead to a runtime error. pb->name[i]pb[i].name不同,并且在您的代码中使用会导致运行时错误。 You're not referencing the i 'th name in your pb array - instead, you're referencing the i 'th character in the name member of pb[0] .您没有引用pb数组中的第iname - 相反,您引用的是pb[0]name成员中的第i字符 You're passing that character value to strcmp , which expects an address value of type char * .您将该字符值传递给strcmp ,它需要一个char *类型的地址值。 It's very unlikely that any regular character value is also a valid address, so you will almost certainly see a runtime error there.任何常规字符值不太可能也是有效地址,因此您几乎肯定会在此处看到运行时错误。

In order to directly answer this question I would suggest to use (*pb).number为了直接回答这个问题,我建议使用 (*pb).number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM