简体   繁体   English

在结构体中使用点运算符和箭头运算符以及字符串指针

[英]Using dot operator vs arrow operator with string pointer in structs

Q1: I'm trying to understand why in this code I'm supposed to use a dot operator when calling the string pointer rather than an arrow which is used for pointers. Q1:我试图理解为什么在这段代码中,我在调用字符串指针时使用点运算符,而不是使用用于指针的箭头。

Q2: If I was trying to initialize age would I use the dot operator? 问题2:如果我尝试初始化年龄,我会使用点运算符吗?

typedef struct Person 
{
  char *name;
  int age;

} Person;

Person* deletePerson(Person *person, int totalPeople) 
{
     int i;

     for(i=0; i<totalPeople; i++) 
     {
     free(person[i].name); 
     free(person); 
     }

     return NULL;
}

You need to use the arrow -> operator when you have a pointer to a struct (or union) on the left , and the dot . 当您在左侧有指向结构(或联合)的指针以及点时,需要使用arrow- ->运算符. operator when you have a struct on the left. 左侧有结构时的运算符。 It doesn't depend on what's on the right. 它不取决于正确的地方。

If person was a pointer to a single Person , to access its field, you'd use person->name and person->age . 如果person是指向单个Person的指针, Person访问其字段,请使用person->nameperson->age

But here person is evidently a pointer to the first element of an array of Person . 但是这里的person显然是一个指向Person数组第一个元素的指针。 (A pointer to the first element is how arrays are passed around in C.) To access an individual element of the array, you use the subscript operator (square brackets […] ): person[i] . (指向第一个元素的指针是数组如何在C中传递。)要访问数组的单个元素,请使用下标运算符(方括号[…] ): person[i] person[i] is the element with index i , not a pointer to the element with index i . person[i]是具有索引的元素i ,不指向与索引元素i Since person[i] is a struct, you use the dot operator to access its fields. 由于person[i]是一个结构,因此您可以使用点运算符来访问其字段。


In addition, as others have already remarked, free(person); 另外,正如其他人已经提到的那样, free(person); inside the loop doesn't make sense. 循环内没有意义。 You'd free the whole array as soon as you've processed the first element. 处理完第一个元素后,就可以释放整个数组。 Call free(person) after the loop, once you've finished cleaning up all the elements of the array (assuming that person was allocated with malloc ). 清理完数组的所有元素后,请在循环后调用free(person) (假设该person已分配了malloc )。

person->name would be synonymous with person[0].name ; person->name将与person[0].name同义; since you want to refer to the name of an arbitrary person , you couldn't use -> this way. 由于您要引用任意person ,因此无法使用->这样的方式。

(I suppose you would use (person+i)->name , but your original code is clearer, IMHO.) (我想您会使用(person+i)->name ,但是您的原始代码更加清晰,恕我直言。)

As for initializing age : what else would you use other than . 至于初始化age :除了还会使用什么. ?

PS free ing person inside the loop means a) you free it multiple times, and b) person[i] will be a problem after the first iteration. 循环内的PS free person意味着a)您多次释放它,并且b) person[i]在第一次迭代后将是一个问题。

For starters the function is invalid and has a bug. 对于初学者来说,该功能无效并且存在错误。

I think you mean 我想你是说

Person* deletePerson(Person *person, int totalPeople) 
{
     for ( int i=0; i < totalPeople; i++ ) 
     {
         free( person[i].name ); 
         free( person[i] ); 
     }

     free( person );

     return NULL;
}

This statement 这个说法

free(person[i].name);

can be equivalently rewriten like 可以像这样重写

free( ( person + i )->name );

or even like 甚至喜欢

free( ( *( person + i ) ).name );

However the first statement is more readable. 但是,第一个语句更具可读性。

According to the C Standard (6.5.2.1 Array subscripting) 根据C标准(6.5.2.1数组下标)

2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. 2后缀表达式后跟方括号[]是数组对象元素的下标名称。 The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) . 下标运算符[]的定义是E1 [E2]与(*((E1)+(E2)))相同 Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero). 由于适用于二进制+运算符的转换规则,如果E1是一个数组对象(相当于一个指向数组对象的初始元素的指针),而E2是一个整数,则E1 [E2]指定E1的第E2个元素E1(从零开始计数)。

In C, the -> operator is a cosmetic substitute for (*var).field, and the [ ] is a substitute for *(ptr + n). 在C语言中,->运算符是(* var).field的替代品,而[]是*(ptr + n)的替代品。

In your code, the line: 在您的代码中,该行:

free(person[i].name);

Is equivalent to: 等效于:

free((*(person + i)).name)

If you use -> instead of the dot operator that would be wrong. 如果使用->代替点运算符,那将是错误的。 I can see why you are confused, to make it look simpler, the function parameter “Person *person” is basically an array of struct Person, equivalent to “Person person[]”. 我可以看到为什么感到困惑,为了使其看起来更简单,函数参数“ Person * person”基本上是struct Person的数组,等效于“ Person person []”。 If you were to access only the first Person in the array you could have used person->name which is the same as person[0].name 如果只访问数组中的第一个Person,则可以使用person-> name,该名称与person [0] .name相同

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

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