简体   繁体   English

为什么在我的C代码中使用x-> y = z而不是xy = z?

[英]Why am I using x->y = z instead of x.y = z in my C code?

I'm following instructions to create code for a flexible array, and I am told to use x->y = z instead of xy = z. 我正在按照说明创建用于灵活数组的代码,并被告知使用x-> y = z而不是xy = z。 What would be the reason to use pointers instead of just assigning values normally? 使用指针而不是正常分配值的原因是什么?

This code includes a custom library flexarray.h which I'll put at the bottom 这段代码包含一个自定义库flexarray.h,我将其放在底部

flexarray.c: flexarray.c:

struct flexarrayrec{
   int capacity;
   int itemcount;
   int *items;
};

flexarray flexarray_new(){
   flexarray result = emalloc(sizeof *result);
   result->capacity = 2;
   result->itemcount = 0;
   result->items = emalloc(result->capacity * sizeof result->items[0]);
   return result;
}

flexarray.h: flexarray.h:

#ifndef FLEXARRAY_H_
#define FLEXARRAY_H_

typedef struct flexarrayrec *flexarray;

extern void flexarray_append(flexarray f, int item);
extern void flexarray_free(flexarray f);
extern flexarray flexarray_new();
extern void flexarray_print(flexarray f);
extern void flexarray_sort(flexarray f);

#endif

x->y is just syntax sugar for this x->y只是为此的语法糖

(*x).y

Looks like flexarray is just typedef for *flexarrayrec , so when you write flexarray result , compiler will "transform" it to 看起来flexarray只是*flexarrayrec typedef,所以当您编写flexarray result ,编译器会将其“转换”为

flexarrayrec *result = emalloc(sizeof(flexarrayrec));

So result.capacity , for example, would have no meaning as pointer is just a number (memory address), "it doesn't have any field". 因此,例如result.capacity就没有意义,因为指针只是一个数字(内存地址),“它没有任何字段”。 (*result).capacity will work, but it's frustrating to write so many asterisks and parentheses, that's why we use -> operator :) (*result).capacity可以使用,但是(*result).capacity星号和括号令人沮丧,这就是为什么我们使用-> operator :)

x->y is equivalent to (*x).y (it is syntactic sugar) x->y等效于(*x).y (它是语法糖)

That is, if x is a pointer. 也就是说,如果x是指针。 If your structure (or object) is not a pointer, you can use xy (and not x->y) 如果您的结构(或对象)不是指针,则可以使用xy(而不是x-> y)

In your case you must use dynamic allocation (pointers), since you want to use flexarray outside of the function flexarray_new(). 在您的情况下,您必须使用动态分配(指针),因为您想在函数flexarray_new()之外使用flexarray。

Your flexarray is a pointer of struct flexarrayrec type. 您的flexarraystruct flexarrayrec类型的指针。 So- 所以-

typedef struct flexarrayrec *flexarray;

When you have structure pointer's you have to use -> arrow operator to access structure members! 当拥有结构指针时,必须使用->箭头运算符来访问结构成员!

But if you have declared 但是如果你已经宣布

 typedef struct flexarrayrec flexarray; // Static declaration

Now you are allowed to use . 现在您可以使用. dot operator to access its members! 点运算符访问其成员!

If you want to use . 如果要使用. means you can follow- 表示您可以遵循-

(*x).y = z instead of x->y = z;

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

相关问题 你如何评估 z = x- - == y + 1; - How do you evaluate z = x- - == y + 1; 表达式 &amp;x-&gt;y-&gt;z 的求值顺序是什么? - What is the evaluation order of the expression &x->y->z? (x &lt;= y &lt;=z) ISO C 中的条件语法? - (x <= y <=z) condition syntax in ISO C? X &lt; <y> &gt; z中的评估顺序 - x<<y>>z order of evaluation in C x-&gt; y在c中是否与c#/ java中的xy相同? - Is x -> y the same in c as x.y in c#/java? 在C中,(x == y == z)的行为与我期望的一样吗? - In C, does (x==y==z) behave as I'd expect? C中针对语句z = ++ x || ++ y &amp;&amp; ++ z的运算符优先级 - Operator precedence in C for the statement z=++x||++y&&++z 如果“x =(date &lt;&lt; 7)&gt;&gt; 12”和{y = date &lt;&lt; 7; z = y &gt;&gt; 12;},为什么x和z的评估方式不同? - Why are x and z evaluating differently if “x=(date<<7)>>12” and {y=date<<7;z=y>>12;}? char * x,y,z; char * x,y,z; char(*)x,y,z ;?之间有什么区别? - What is the difference between char * x,y,z;char* x,y,z;char (*)x,y,z;? 将结构从x-&gt; y转换为xy并删除malloc时出现Valgrind错误。 努力学习C(前16) - 额外学分 - Valgrind errors when converting structs from x->y to x.y and removing malloc. Learn C the hard way (ex 16) - extra credit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM