简体   繁体   English

动态数组堆栈结构C

[英]Dynamic array stack struct C

I have a couple of questions. 我有一些问题。 I have a bit of a hard time understanding this code. 我在理解此代码方面有些困难。 What exactly is it doing? 它到底在做什么?

For example: 例如:

What does typedef struct dynArrStruct do and why does it have dynArr at the end of it? typedef struct dynArrStruct做什么,为什么它的末尾有dynArr? I know the definition of typedef as "allows to created alias for a known data type" but that is jargon to me. 我知道typedef的定义是“允许为已知的数据类型创建别名”,但这对我来说是行话。 Can someone try to put it in layman terms? 有人可以用外行人的话来表达吗? Thank you! 谢谢!

Why are there 2 struct variables (a1/a2)? 为什么会有2个结构变量(a1 / a2)?

Link to full code if needed: 链接到完整代码(如果需要):

http://www.cs.uic.edu/pub/CS211/CS211LectureNotesS13/dynArr.c http://www.cs.uic.edu/pub/CS211/CS211LectureNotesS13/dynArr.c

typedef struct dynArrStruct
{
    double *location;
    int length;
    int currSize;
} dynArr;

int main (int argc, char**argv)
{
    struct dynArrStruct a1;
    dynArr a2;
    int i;

    //rest of code
}

What does typedef struct dynArrStruct do and why does it have dynArr at the end of it? typedef struct dynArrStruct做什么,为什么它的末尾有dynArr

The typedef creates an alias to a type to save you some typing, or to improve readability. typedef为类型创建别名,以节省您的键入时间或提高可读性。 In this particular case, it creates an alias called dynArr for the struct dynArrStruct . 在这种特殊情况下,它将为struct dynArrStruct创建一个名为dynArr的别名。

Without a typedef , ie with only this 没有typedef ,即只有这个

struct dynArrStruct
{
    double *location;
    int length;
    int currSize;
};

you would be forced to write struct dynArrStruct every time you need to declare a variable of that struct 's type. 每当您需要声明该struct类型的变量时,您将被迫编写struct dynArrStruct With a typedef in place, you can write simply dynArr , and the compiler will interpret it as the struct dynArrStruct for you. 使用typedef时,您可以简单地编写dynArr ,编译器将为您解释为struct dynArrStruct

typedef struct dynArrStruct
{
    double *location;
    int length;
    int currSize;
} dynArr;

Is a short form of two different pieces of code. 是两个不同代码段的缩写。

// define a struct by name dynArrStruct
struct dynArrStruct
{
    double *location;
    int length;
    int currSize;
}; 

//Example of use
struct dynArrStruct d1;

and

// define an alias to "struct dynArrStruct" called dynArr
typedef struct dynArrStruct dynArr; 


//Example of use
dynArr d2; //whoa so short!

In addition to dasblinkenlight's answer, 除了dasblinkenlight的答案之外,

Why are there 2 struct variables (a1/a2)?

The code presented appears to be an example of poorly modularised code (a1) and well modularised code (a2). 呈现的代码似乎是模块化程度较低的代码(a1)和模块化程度良好的代码(a2)的示例。 The modifications made to a1 are very similar to the modifications made to a2 . a1的修改与对a2的修改非常相似。 However, the modifications made to a2 are abstracted out into functions (lines 53-55 correspond to the lines found in init , 57-58 correspond to the lines found in push and push ), so that that functionality can be easily reused. 但是,对a2所做的修改被抽象为函数(第53-55行对应于init找到的行,第57-58行对应于pushpush找到的行),以便可以轻松地重用功能。 An example of this reuse is lines 69-72. 第69-72行是这种重用的一个例子。

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

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