简体   繁体   English

为什么指向结构的指针表现得像一个结构数组

[英]why is pointer to struct behaving like an array of structs

I have the following code我有以下代码


typedef struct{
    int fildes;
    char key[MAX_KEYLEN];
} item_t;

static int nitems;
static item_t *items;
FILE *filelist;
    int capacity = 16;
    char *path, *ptr;

    if( NULL == (filelist = fopen(filename, "r"))){
        fprintf(stderr, "Unable to open file in content_init.\n");
        exit(EXIT_FAILURE);
    }

    items = (item_t*) malloc(capacity * sizeof(item_t));
    nitems = 0;
    while(fgets(items[nitems].key, MAX_KEYLEN, filelist)){
        /*Taking out EOL character*/
        items[nitems].key[strlen(items[nitems].key)-1] = '\0';
// and there's more code below which is not relevant to the question

In the above code, Item_t is a struct defined as below在上面的代码中, Item_t是一个定义如下的结构体

typedef struct{
    int fildes;
    char key[MAX_KEYLEN];
} item_t;

Then, items is defined as然后, items被定义为
static item_t *items;

items is initialized using the following code使用以下代码初始化items
items = (item_t*) malloc(capacity * sizeof(item_t));

Then, the following is done with items然后,对物品进行以下操作
fgets(items[nitems].key, MAX_KEYLEN, filelist)

items is supposed to be a struct. items 应该是一个结构体。 How did it become an array?怎么变成数组了? I said array because, items[nitems] is being done which made me feel that items is an array of item_t structs我说数组是因为正在完成items[nitems]这让我觉得 items 是一个 item_t 结构的数组

Pointers and arrays can (for the most part) be used interchangeably.指针和数组(在大多数情况下)可以互换使用。
array[i] is just syntatic sugar for *(array+i) . array[i]只是*(array+i)语法糖。

Answer to your question "why is pointer to struct behaving like an array of structs ?"回答您的问题“为什么指向 struct 的指针表现得像一个 struct 数组?” (The answer below is applicable to any data type regardless of it being structure or some other data type) (下面的答案适用于任何数据类型,无论它是结构还是其他数据类型)

When you initialize an array (of any data type) with 'N' elements you are simply asking for memory of size equal to (N * sizeof(data-type)) whose base address is the address of 1st element in the array (ie element at index zero).当您用 'N' 个元素初始化一个(任何数据类型的)数组时,您只是要求大小等于(N * sizeof(data-type))的内存,其基地址是数组中第一个元素的地址(即索引为零的元素)。

So when you access any element in an array what you are effectively doing is de-referencing a value stored at particular address in memory.因此,当您访问数组中的任何元素时,您实际上所做的是取消引用存储在内存中特定地址的值。

Example:例子:

#define N 3
int32_t array[N] = {11, 22, 33};

Suppose now I want to print the last element in the array (ie element at index 2).假设现在我想打印数组中的最后一个元素(即索引 2 处的元素)。 Since I know the base address of array, I can access the elements in the array in following ways:由于我知道数组的基地址,我可以通过以下方式访问数组中的元素:

printf("%d \n", array[2]);
/***** OR *****/
printf("%d \n", *(array + 2));

NOTE:笔记:

In second printf() from machine's perspective what is happening is this:从机器的角度来看,在第二个printf() ,正在发生的事情是这样的:

*( base-address + index * sizeof(data-type) ) *( 基地址 + 索引 * sizeof(data-type) )

since from machine's perspective the concept of data-type, array, etc doesn't exist.因为从机器的角度来看,数据类型、数组等的概念不存在。 (Simplified explanation without touching the assembly level code) (不触及汇编级代码的简化解释)

NOTE:笔记:

array[i] is just a syntactic-sugar for *(array + i) array[i]只是*(array + i)的语法糖

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

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