制作一个数组来保存字符数组的数组用C [英] Making an Array to Hold Arrays of Character Arrays in C

查看:204
本文介绍了制作一个数组来保存字符数组的数组用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C比此刻生锈的多一点,所以我没有创造的东西,我认为应该是pretty基础。

My C is a little more than rusty at the moment, so I'm failing to create something I think should be pretty basic.

请允许我引用字符数组作为这篇文章的字符串。这将使事情为我和你更清晰。

Allow me to refer to character arrays as strings for this post. It will make things clearer for both me and you.

我有什么是可以容纳1个或多个字符串数组。例如{AB,CD,EF}。我不想再拍数组来存储字符串数组的多个版本。因此,像{{AB,CD,EF},{GH,IJ},{KL}}

What I have is an array that can hold 1 or more strings. For instance {"ab", "cd", "ef"}. I want to make another array to store multiple versions of the array of strings. So something like {{"ab", "cd", "ef"}, {"gh", "ij"}, {"kl"}}.

我现在所拥有的是:

char *arrayOfStrings[50]; // a single array to hold multiple strings
char **arrayOfArraysOfStrings[10]; // array to hold multiple snapshots of arrayOfStrings

随着时间的字符串的变化和我使用的字符串数组的数组基本上持有arrayOfStrings的历史快照的阵列。当我需要访问数据快照阵列我的问题就来了。这是code我必须把东西到快照数组:

The array of strings changes over time and I'm using the array of arrays of strings to basically hold historical snapshots of arrayOfStrings. My problem comes when I need to access the snapshot array for data. This is the code I have to put stuff into the snapshot array:

arrayOfArraysOfStrings[input_index] = arrayOfStrings; // you can assume input_index is updated correctly, I've already checked that.

这似乎是不正确的,因为当我尝试访问并打印出快照数组的内容,只打印出从最近arrayOfStrings的信息。我会与此的想法是,让他们放置arrayOfChars在快照阵列的条目指向,所以我以后可以访问它的地址。

This appears to be incorrect since when I try to access and print out the contents of the snapshot array, it only prints out the information from the most recent arrayOfStrings. The idea I was going for with this was to store the address of where arrayOfChars is pointing in an entry of the snapshot array so I can access it later.

目前,对快照的访问数组中的一个条目来完成这样的:

Right now, access to an entry in the snapshot array is accomplished like this:

arrayOfArraysOfChars[historicalIndex][indexOfTargetChar]

有我期待回答几个问题:

There are several questions I'm looking to answer:


  1. 是我列出适合什么,我试图做或方法有一个缺陷在我的整个逻辑?

  2. 我在做什么毛病我目前的code,我如何解决这个问题?

  3. 有没有更好的方式来做到这一点,如果是的话,请问该数组初始化,除了阵列,并从阵列工作在看什么?

- 编辑4 / 18--
问题的部分原因是,我设置的指针在arrayOfArraysOfStrings为指向arrayOfStrings指向同样的事情。既然arrayOfStrings被编辑过的不好。我需要一些方法简单地分配一个新的内存块arrayOfStrings在指向复制一个二维数组... preferably。

--edit 4/18-- Part of the problem is that I'm setting pointers in arrayOfArraysOfStrings to point to the same thing that arrayOfStrings is pointing to. That's bad since arrayOfStrings gets edited. I need some way to duplicate a 2D array... Preferably by simply allocating a new block of memory for arrayOfStrings to point at.

推荐答案

您必须在这两个的阵列中的一个太很多三分球。

You have one-too-many pointers in both of your arrays.

char arrayOfChars[50]; // a single array of characters
char *arrayOfArraysOfChars[10]; // array to hold multiple single arrays of characters

由于正在使用像一个缓冲arrayOfChars(新的数据总是会去那里第一),你需要将字符串的副本保存到arrayOfArrays。 POSIX的功能的strdup 应有助于在这里。

通知&安培; * 是对立的,所以&放大器; * *放大器; 什么都不做。

Notice & and * are opposites, so &* and *& do absolutely nothing.

您也可以,使arrayOfArrays字面上说。

You could also, make the arrayOfArrays literally that.

char arrayOfChars[50]; // a single array of characters
char arrayOfArraysOfChars[10][50]; // array to hold multiple single arrays of characters

通过这种设置,你应该使用的strcpy 将数据复制到arrayOfArrays。

With this setup, you should use strcpy to copy the data into arrayOfArrays.

看了你的编辑,我认为你需要开始的真正的简单。和FWIW变量名是一种错误的匈牙利的。

Having read your edit, I think you need to start real simple. And FWIW the variable names are the wrong kind of Hungarian.

对于我认为你想要做什么,我只是一个单一的字符数组开始。这将是正在输入,并检查了主要的缓存的,持有的字符串。

For what I think you're trying to do, I'd start with just a single char array. This will be the main buffer, to hold strings that are being input and examined.

enum { BUFSZ = 50 };
char buf[BUFSZ + 1];

然后你可以用与fgets 或任何使用它。

fgets(buf, BUFSZ, infile);

要起来数组保存这些,我会使用的strdup 其自动修整。如果字符串将是大多是2个字符长,我不想被用于每一个48额外的字节。所以,char指针(字符串)数组。

To save these up in an array, I'd use strdup for its automatic trimming. If the strings are going to be mostly 2 characters long, I don't want 48 extra bytes being used for each one. So, an array of char pointers (strings).

enum { STRVSZ = 40 };
char *strv[STRVSZ + 1];
int i;
i = 0;
strv[i] = strdup(buf);
strv[i+1] = NULL; // This makes it an "argv-style" NULL-terminated array of strings
++i; // i is now the index of the next element, and a count of elements already added

STRV 的每个元素都是一个字符指针。但要preserve我们的理智,我们正在努力的抽象掉的一些细节分心了一会儿,并且治疗的字符串的作为一个单独的数据类型。

Each element of strv is a char pointer. But to preserve our sanity, we're trying to abstract away some of that distracting detail for a moment, and treat strings as a separate data type.

现在创建的这些名单,我们再次做同样的事情。但是,没有任何的strdup 型功能,使指针数组的副本,所以我们要分开的分配和复制。

Now to create lists of these, we do the same thing again. But there's no strdup-type function to make a duplicate of an array of pointers, so we have to separate the allocation and copying.

enum { STRVVSZ = 20 };
char **strvv[STRVVSZ + 1];
int j;
j = 0;
strvv[j] = calloc(i+1, sizeof *strvv[j]); // assuming i is the count of elements 
memcpy(strvv[j], strv, i * sizeof *strvv[j]);
++j; // j is now the index of the next string-pointer array in the array-of-same,
     // and a count of elements already added.

现在,我的名字是一样的你傻傻的,但他们更短的

Now, my names are just as silly as yours, but they're shorter!

这篇关于制作一个数组来保存字符数组的数组用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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