c - C 中的动态数组 vector

标签 c pointers vector dynamic-arrays

● int vectorInsert( vector * 数组, int 索引, 数据值);

我在做

如果可以根据给定的声明更正此问题。

我正在使用它调用

Vector *vect = initVector();
Data data_array[20];
for(i = 0 ; i < 20 ; i++){
    data_array[i].value = (rand() % 20) + 1;
    vectorInsert(vect, i, data_array[i]);
}

最佳答案

您的代码中有几个错误,但最重要的一个是在您的 initVector 函数中,您实际上需要为 vector 分配内存。

您还需要执行以下操作:

  • initVector 中返回 v 而不是 v->data&v
  • vectorInsert中打印array->data[index].value而不是array->data[index]
  • vectorInsert 中,成功时返回 1,在分配中添加错误检查,并在内存错误时返回 0

除了原始的 malloc 之外,所有这些都是编译器返回的警告。

关于c - C 中的动态数组 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40403889/

相关文章:

从 Fortran 调用带有 **int 参数的 C 函数

c - C语言如何创建指向位的指针

c - 如何初始化一个 wchar_t 变量?

c - 如何在C中逐行将文件内容读入结构

C 编译器忽略 'static' 的结构声明

c++ - 在 C++11 中使用什么更好,零或 NULL?

python - Python 中的 C 指针算法

c++ - 如何从头到尾遍历 vector ?

javascript - Paper JS : Wavy Line, 沿着路径刷

c++ - 如何找出std::vector中是否存在某个元素?