vector_int.h 是一个带有自制动态数组(向量)结构的头文件。
test.c 是一个测试程序。
所有代码如下:
向量int.h:
#include <stdio.h>
typedef struct
{
long int len; // Length
int *array; // Dynamic Array
} IntVector;
void ResizeIntVector(IntVector *vector, int size) // Resizing of vector
{
realloc(vector->array, size * sizeof(int));
vector->len = size; // Changing of length variable
}
void SetIntVectorCell(IntVector *vector, unsigned int cell_number, int cell_value) // Put cell_value in array[cell_number]
{
if (cell_number >= vector->len)
ResizeVectorInt(&vector, cell_number); // Grow size of memory if it's not enough
vector->array[cell_number] = cell_value;
}
测试.c:
#include "vector_int.h"
#include <stdio.h>
int main()
{
IntVector vector;
int n;
scanf("%d", &n);
int i;
for (i = 0; i < n; i++) // testing
{
SetIntVectorCell(&vector, i, i);
printf("%d ", vector.array[i]);
}
return 0;
}
日志:
1 0 D:\Work\Raspberry Pi\test.c In file included from D:\Work\Raspberry Pi\test.c
D:\Work\Raspberry Pi\vector_int.h In function 'ResizeIntVector':
11 2 D:\Work\Raspberry Pi\vector_int.h [Warning] incompatible implicit declaration of built-in function 'realloc' [enabled by default]
[Linker error] C:\Users\ALEXAN~1\AppData\Local\Temp\cccFKqxs.o:test.c:(.text+0x4a): undefined reference to `ResizeVectorInt'
collect2: ld returned 1 exit status
我认为使用 realloc 函数有错误,但我认为我做得很好。请帮助我并找出错误或错误。