如何使下的动态大小的数组? [英] How to make a dynamic sized array in C?

查看:151
本文介绍了如何使下的动态大小的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了PHP,但我开始学习C.我试图创建一个程序,它逐行读取并存储每一行​​的文件行的数组。

I'm used to PHP, but I'm starting to learn C. I'm trying to create a program that reads a file line by line and stores each line to an array.

到目前为止,我有一个程序,逐行读取文件中的行,甚至因为它去打印每行,但现在我只需要每行添加到一个数组。

So far I have a program that reads the file line by line, and even prints each line as it goes, but now I just need to add each line to an array.

我的好友昨晚告诉我一点关于它。他说,我不得不使用在C多维数组,所以基本上数组[X] [Y] 。在 [Y] 部分本身是很容易的,因为我知道,每一个行会的最大字节数量。但是,我不知道有多少的的文件会。

My buddy last night was telling me a bit about it. He said I'd have to use a multidimensional array in C, so basically array[x][y]. The [y] part itself is easy, because I know the maximum amount of bytes that each line will be. However, I don't know how many lines the file will be.

我想我可以把它遍历文件,每次只需增加一个整数并使用,但我觉得有可能是做的更简单的方法。

I figure I can make it loop through the file and just increment an integer each time and use that, but I feel that there might be a more simple way of doing it.

在正确的方向任何想法,甚至一个暗示?我AP preciate任何帮助。

Any ideas or even a hint in the right direction? I appreciate any help.

推荐答案

要动态分配一个二维数组:

To dynamically allocate a 2D array:

char **p;
int i, dim1, dim2;


/* Allocate the first dimension, which is actually a pointer to pointer to char   */
p = malloc (sizeof (char *) * dim1);

/* Then allocate each of the pointers allocated in previous step arrays of pointer to chars
 * within each of these arrays are chars
 */
for (i = 0; i < dim1; i++)
  {
    *(p + i) = malloc (sizeof (char) * dim2);
   /* or p[i] =  malloc (sizeof (char) * dim2); */
  }

 /* Do work */

/* Deallocate the allocated array. Start deallocation from the lowest level.
 * that is in the reverse order of which we did the allocation
 */
for (i = 0; i < dim1; i++)
{
  free (p[i]);
}
free (p);

修改上述方法。当你需要另一条线路加做 *(P + I)=的ma​​lloc(sizeof的(char)的* DIM2); 和更新 I 。在这种情况下,你需要predict文件中的最大行数这是由 DIM1 变量表示,为此我们分配 p 数组的第一次。这只会分配(sizeof的为(int *)* DIM1)字节,比字符p从而更好的选择[DIM1] [DIM2] (以C99)。

Modify the above method. When you need another line to be added do *(p + i) = malloc (sizeof (char) * dim2); and update i. In this case you need to predict the max numbers of lines in the file which is indicated by the dim1 variable, for which we allocate the p array first time. This will only allocate the (sizeof (int *) * dim1) bytes, thus much better option than char p[dim1][dim2] (in c99).

有另一种方式,我认为。分配阵列块和连锁他们时存在溢出。

There is another way i think. Allocate arrays in blocks and chain them when there is an overflow.

struct _lines {
   char **line;
   int n;
   struct _lines *next;
} *file;

file = malloc (sizeof (struct _lines));
file->line = malloc (sizeof (char *) * LINE_MAX);
file->n = 0;
head = file;

在此之后的第一个块就可以使用了。当你需要插入一行只是做:

After this the first block is ready to use. When you need to insert a line just do:

/* get line into buffer */
file.line[n] = malloc (sizeof (char) * (strlen (buffer) + 1));
n++;

N LINE_MAX 分配另一个块,并将其链接到了这里。

When n is LINE_MAX allocate another block and link it to this one.

struct _lines *temp;

temp = malloc (sizeof (struct _lines));
temp->line = malloc (sizeof (char *) * LINE_MAX);
temp->n = 0;
file->next = temp;
file = file->next;

这样的事情。

在一个街区的 N 变成 0 ,释放它,并更新当前块的指针文件到previous之一。您可以遍历从创业之初单链表和遍历或使用双链路。

When one block's n becomes 0, deallocate it, and update the current block pointer file to the previous one. You can either traverse from beginning single linked list and traverse from the start or use double links.

这篇关于如何使下的动态大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆