我的程序解析代码,并且在解析代码时,我需要跟踪每个关键字通过每x行(可能每10行左右)经过多少个实例,这些信息我计划在以后创建带有gnuplot的直方图,但这并不重要。
所以有13个关键字,我可以很容易地用0的数组来计算它们,其中每个索引代表一个关键字,每当我找到一个关键字时,我将它的索引增加1.好的简单
int keywordcount[13]={0};
问题是我需要每10行创建一个新的keywordcount
,我不知道文件有多少代码行。所以这告诉我,我应该有一个keywordcount
数组的动态数组。
如何在C中声明这个动态数组数组,以及如何添加它keywordcount
数组?我仍然对C中的多维数组感到困惑。我不知道是否应该将它声明为指针数组或者什么,我不知道如何将它分配给新的keywordcount
数组当创建keywordcount
的函数返回时,它不会消失。如果有什么不清楚,请告诉我。
答案 0 :(得分:2)
假设将创建多少keywordcount
个。假设您几乎可以肯定会创建其中的10个。
您可以动态声明一个包含10行和13列的2D数组。
现在keywordcount[0]
将是第一个keywordcount数组(大小为13)的索引,依此类推。
现在,如果您在行动中发现需要10个以上的keywordcount数组,则可以使用realloc()
动态增加2D数组的大小。
答案 1 :(得分:2)
您可以使用malloc()
或calloc()
创建静态数组的动态数组。例如,这将keywordcount_arr
定义为指向13 int
的数组的指针(这里为max_lines
这样的数组分配了足够的内存):
size_t max_lines = 10;
int (*keywordcount_arr)[13] = calloc(max_lines, sizeof *keywordcount_arr);
如果在这里使用typedef
,它可能会使代码更容易读写:
typedef int KW_Count[13];
/* ... */
KW_Count *keywordcount_arr = calloc(max_lines, sizeof *keywordcount_arr);
您可以使用二维数组索引编入已分配的内存:
for (size_t i = 0; i < 13; i++) {
keywordcount_arr[0][i] = i;
}
或者,如果现有数组必须存储在动态数组中,则可以使用memcpy()
。如果动态数组需要增长,则可以使用realloc()
。并且realloc()
可以再次用于将动态分配修剪为最终大小:
max_lines *= 2;
KW_Count *temp = realloc(keywordcount_arr,
sizeof *keywordcount_arr * max_lines);
以下是一个示例程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_KEYWORDS 13
typedef int KW_Count[NUM_KEYWORDS];
int main(void)
{
size_t max_lines = 10;
size_t num_lines = 0;
KW_Count *keywordcount_arr = calloc(max_lines, sizeof *keywordcount_arr);
if (keywordcount_arr == NULL) {
perror("Allocation failure");
/* Handle error... perhaps: */
exit(EXIT_FAILURE);
}
/* Store keyword counts directly in keywordcount_arr[] */
++num_lines;
for (size_t i = 0; i < NUM_KEYWORDS; i++) {
keywordcount_arr[0][i] = i;
}
/* Or use memcpy() if an existing array must be stored */
++num_lines;
KW_Count keywordcount = { 0, 2, 0, 0, 3, 1, 0, 0, 0, 1, 2, 0, 1 };
memcpy(keywordcount_arr[1],
keywordcount,
sizeof *keywordcount * NUM_KEYWORDS);
/* Use realloc() to grow the dynamic array as needed */
max_lines *= 2;
KW_Count *temp = realloc(keywordcount_arr,
sizeof *keywordcount_arr * max_lines);
if (temp == NULL) {
perror("Unable to reallocate");
/* Handle error */
} else {
keywordcount_arr = temp;
}
/* Use realloc() to trim the dynamic array to final size */
temp = realloc(keywordcount_arr, sizeof *keywordcount_arr * num_lines);
if (temp == NULL) {
perror("Unable to reallocate");
/* Handle error */
} else {
keywordcount_arr = temp;
}
/* Display array contents */
for (size_t i = 0; i < num_lines; i++) {
for (size_t j = 0; j < NUM_KEYWORDS; j++) {
printf("%5d", keywordcount_arr[i][j]);
}
putchar('\n');
}
/* Cleanup */
free(keywordcount_arr);
return 0;
}
节目输出:
0 1 2 3 4 5 6 7 8 9 10 11 12
0 2 0 0 3 1 0 0 0 1 2 0 1
答案 2 :(得分:-1)
请勿触摸2D数组,数组数组,指针数组,指向数组的指针或任何此类废话。
将您的静态大小的数组包裹在struct
。
typedef struct
{
int keyword_count[13];
} fragment_info;
拥有fragment_info
的动态数组,就像创建任何其他动态数组一样:
fragment_info* infos = malloc(initial_capacity * sizeof(*infos));
....
fragment_info* new_infos = realloc(infos, new_capacity * sizeof(*new_infos));
现在,如果您想存储有关10行片段的其他信息,您可以自然地保留它。只需在struct
。