在c中如何迭代字符数组的数组?

发布于 2024-12-29 09:51:47 字数 100 浏览 0 评论 0原文

您是否必须手动循环一次数组并获取每个字符数组的 strlen 计数,对其求和,使用求和值分配目标,然后再次循环数组?

如何找到包含字符数组的数组的大小,以便可以迭代它们?

Do you have to manually loop through the array once and get a count of the strlen of each character array, sum it, allocate destination with the summed value and then loop over the array again?

How do you find the size of the array that contains the arrays of characters so you can iterate over them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骑趴 2025-01-05 09:51:47

如何找到包含字符数组的数组的大小,以便可以迭代它们?

有两种方法:

  1. 在变量中分配数组时记录数组中字符串的数量。
  2. 在数组末尾分配一个额外的 char* 并在其中存储一个空指针作为标记,类似于使用 NUL 字符终止字符串的方式。

换句话说,在分配数组时您必须自己进行簿记,因为 C 不会为您提供所需的信息。 获取字符串数组中的字符总数。

size_t sum_of_lengths(char const **a)
{
    size_t i, total;
    for (i = total = 0; a[i] != NULL; i++)
        total += strlen(a[i]);
    return total;
 }

如果您遵循第二个建议,则可以通过在进行实际串联时不要忘记为 '\0' 保留空间来

How do you find the size of the array that contains the arrays of characters so you can iterate over them?

There are two ways:

  1. Record the number of strings in the array when you allocate it in a variable.
  2. Allocate an extra char* at the end of the array and store a null pointer in it as a sentinel, similar to the way a NUL character is used to terminate a string.

In other words, you have to do your own bookkeeping when allocating the array because C won't give you the desired information. If you follow the second suggestion, you can get the total number of characters in the array of strings with

size_t sum_of_lengths(char const **a)
{
    size_t i, total;
    for (i = total = 0; a[i] != NULL; i++)
        total += strlen(a[i]);
    return total;
 }

Don't forget to reserve space for a '\0' when doing the actual concatenation.

画▽骨i 2025-01-05 09:51:47

我假设您正在尝试创建一个由数组中所有字符串串联而成的字符串。

有 2 种方法可以执行此操作:

  1. 按照您的建议进行 2 遍,对第一遍中的长度求和,分配目标字符串,然后在第二遍中附加字符串

  2. 制作 1 遍。首先将缓冲区分配到某个大小。附加字符串,记录总大小。如果没有足够的空间容纳字符串,请使用 realloc() 重新分配缓冲区。最有效的重新分配方法是每次将缓冲区大小加倍。

I assume you are trying to make a string that is the concatenation of all of the strings in the array.

There are 2 ways of doing this:

  1. Make 2 passes as you suggest, summing the lengths in the first pass, allocating the destination string, and then appending the strings in the second pass

  2. Make 1 pass. Start by allocating the buffer to some size. Append the strings, keeping track of the total size. If you don't have enough room for a string, reallocate the buffer with realloc(). The most efficient method of reallocation will be to double the size of the buffer each time.

秉烛思 2025-01-05 09:51:47

我猜你想连接字符串。如果是这样,是的。在分配空间之前,您必须知道需要多少空间。

事实上,你可以使用realloc,但它实际上只是每次都复制以前的字符串,而且效率低得多。

一些代码:(假设 char *s[]int n

int i,l=1;
for (i=0;i<n;i++) l+=strlen(s[i]);
char *r=malloc(l);
r[0]=0;
for (i=0;i<n;i++) strcat(r,s[i]);

编辑:正如一些注释,当您知道长度时,strcat 无效。 (我仍然更喜欢它,因为它一次性分配内存。)一些更有效的代码是:

int i,l=1;
for (i=0;i<n;i++) l+=strlen(s[i]);
char *r=malloc(l);
char *d=r;
for (i=0;i<n;i++) {
 srtcpy(d,s[i]);
 d+=strlen(s[i]);
}

I guess that you want to concatenate the strings. If so, yes. You have to know how much space you want before you allocate it.

In fact, you can use realloc, but it's really just copy the previous string every time, and much less effective.

Some code : (assuming char *s[] and int n)

int i,l=1;
for (i=0;i<n;i++) l+=strlen(s[i]);
char *r=malloc(l);
r[0]=0;
for (i=0;i<n;i++) strcat(r,s[i]);

Edit: As some comments, strcat is ineffective when you know the length. (I still prefer it since it allocate the memory in one time.) Some more effective code is:

int i,l=1;
for (i=0;i<n;i++) l+=strlen(s[i]);
char *r=malloc(l);
char *d=r;
for (i=0;i<n;i++) {
 srtcpy(d,s[i]);
 d+=strlen(s[i]);
}
君勿笑 2025-01-05 09:51:47
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *nstrdup(char **args);
int main (int argc, char **argv)
{
char * this;

this = nstrdup(argv+1);
printf("[%s]\n", this );

return 0;
}

char *nstrdup(char **args)
{
size_t len, pos;
char **pp, *result;

len = 0;
for (pp = args; *pp; pp++) {
        len += strlen (*pp);
        }
result = malloc (1+len);

pos = 0;
for (pp = args; *pp; pp++) {
        len = strlen (*pp);
        memcpy(result+pos, *pp, len);
        pos += len;
        }
result[pos] = 0;
return result;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *nstrdup(char **args);
int main (int argc, char **argv)
{
char * this;

this = nstrdup(argv+1);
printf("[%s]\n", this );

return 0;
}

char *nstrdup(char **args)
{
size_t len, pos;
char **pp, *result;

len = 0;
for (pp = args; *pp; pp++) {
        len += strlen (*pp);
        }
result = malloc (1+len);

pos = 0;
for (pp = args; *pp; pp++) {
        len = strlen (*pp);
        memcpy(result+pos, *pp, len);
        pos += len;
        }
result[pos] = 0;
return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文