简体   繁体   English

动态分配向量C

[英]Dynamically allocating a vector C

I'm trying to understand the concept of dynamically allocating a `struct´ in C, also I am interested in generic dynamically allocating, could you give me some help or information about this topic? 我正在尝试了解在C中动态分配“结构”的概念,我也对泛型动态分配感兴趣,您能给我一些有关此主题的帮助或信息吗?

I've managed to understand the concept of pointers and I managed to to some kind of code but I am not sure if is right or if it's working as it should be... 我已经设法理解了指针的概念,并且尝试了某种代码,但是我不确定是否正确或是否可以正常工作……

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    typedef struct {
        int id;
        char nume[20];
    } pers;

    pers *i = malloc(sizeof(pers)*100);

    i->id=22;
    i++;
    i->id=33;
}

My question is, have I declared this array properly, how do I print an element of this array, how do I refer to a specific element for eq. 我的问题是,是否已正确声明此数组,如何打印此数组的元素,如何为eq引用特定元素。 i[9] , and is there another way to do the same thing? i[9] ,还有另一种方法可以做同样的事情吗? I've heard about generic dynamically allocating memory, could someone give me an eq. 我听说过通用动态分配内存,有人可以给我一个答案。 of that? 其中?

When you do i++ you loose the original pointer. 当您执行i++您会松开原始指针。 Instead you can use normal array indexing: 相反,您可以使用常规数组索引:

i[0].id = 22;
i[1].id = 33;

You need the original pointer when you later want to free the allocated memory. 以后要释放分配的内存时,需要原始指针。

以下所有都是同义词:

*(i + 9)

*(9 + i)

i[9]

9[i]

You refer to an element in the dynamically allocated array the same way you would refer to an element in an automatic array. 您引用动态分配数组中的元素的方式与引用自动数组中元素的方式相同。

Using pers[i] will refer to the ith element. 使用pers[i]将引用第ith个元素。 To access a field such as id you would use pers[i].id . 要访问诸如id的字段,您可以使用pers[i].id

Normally you would do the typedef struct before (outside) the function but it works. 通常,您可以在typedef struct之前(外部)执行typedef struct ,但是它可以工作。

Your allocation is correct. 您的分配是正确的。

But i++ makes no sense here. 但是i++在这里没有任何意义。 You should keep the pointer i so that you still know where the allocated memory starts. 您应该保留指针i以便您仍然知道分配的内存从哪里开始。 What you probably want is another pointer into the array, and increase that: 您可能想要的是另一个指向数组的指针,并将其增加:

pers* it = i;
it->id=22;
printf("%d", it->id);
it++;
it->id=33;
printf("%d", it->id);

You shouldn't change the pointer that malloc returned you. 您不应该更改malloc返回的指针。 Treat it like a constant to avoid creating a memory leak (allocating and losing the pointer to the allocated memory). 将其视为常量,以避免产生内存泄漏 (分配和丢失指向已分配内存的指针)。 Instead of modifying i , iterate using a new index variable, let's call it j : To clear/initialize all array elements: 不用修改i ,而是使用新的索引变量进行迭代,我们将其称为j :要清除/初始化所有数组元素:

#include <string.h> /* for memset(). */
int j;
for (j=0; j<100; ++j) {
  i[j].id = 0;
  memset (i[j].nume, 0, 20); /* Or sizeof(i[0].nume) instead of 20. */
}

To print all array elements: 要打印所有数组元素:

for (j=0; j<100; ++j) {
  printf ("id[%d] = %d\n", j, i[j].id);
  printf ("nume[%d] = '%s'\n", j, i[j].nume);
}

I would also suggest to rename i to person ; 我还建议将i重命名为person this makes the code so much more readable. 这使得代码更具可读性。

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

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