如何在 C 循环中将字符串追加到字符串数组中?

发布于 2025-01-13 05:17:12 字数 343 浏览 2 评论 0原文

鉴于每次添加的值(因此指针指向的值正在改变),我正在努力了解如何将字符串附加到字符串数组中。我期望 array 的格式为 {"hello, "helloo", ...} 但我无法让它工作。我可以进行哪些更改使 array 正确存储它?

int size = 10;
char *string = "hello";
char c = "o";
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = string;
}

I'm struggling to see how I can append a string to an array of strings, given that the value being added to each time (therefore the value the pointer points to is changing). I'm expecting array to be in the format {"hello, "helloo", ...} but I can't get this to work. What changes can I make so that array stores this correctly?

int size = 10;
char *string = "hello";
char c = "o";
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = string;
}

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

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

发布评论

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

评论(2

末骤雨初歇 2025-01-20 05:17:12

哎呀...这里有很多错误!

让我们回到基础知识:C 语言没有字符串的概念。在语言级别,您只有(单个)字符类型:char。在库级别,字符串由以 null 结尾的字符数组表示。

因此,为了能够向字符串添加字符,您必须有一个正确维度的数组。顺便说一句,像 "hello" 这样的文字是一个 const 字符数组:程序员无法对其进行任何更改。

因此,这里您希望能够向“hello”添加 1 到 10 个字符,因此您需要一个 16 个字符的数组:5 个字符用于 hello,10 个用于附加 o,1 个用于 null。但这还不是全部,array 只是一个指针数组。如果 array 的所有元素都指向您添加字符的同一个字符串,那么最后它们都将具有完全相同的值!必须让数组的每个元素指向不同的字符数组。假设 strdup 可用,您可以编写:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = strdup(string);  // build a dynamic copy
}

当您不再需要 array 时,您应该释放使用 strdup 分配的所有字符串:

for (int i = 0; i < 10; i++) {
    free(array[i]);
}

BTW,strdup 自 C23 起仅存在于标准 C 库中,之前它只是一个 POSIX 扩展。如果在您的系统上不可用,您可以自行创建:

char *strdup(const char *src) {
    char *dest = malloc(1 + strlen(src));
    strcpy(dest, src);
    return dest;
}

或者您可以通过使用真正的二维数组来避免动态分配:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size][16];      // this is an array of 10 strings of size 16

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    strcpy(array[i], string);
}

Oops... there are tons of errors here!

Let us go back to the basics: C language has no notion of what a string could be. At the language level you only have the (single) character type: char. At the library level, a string is representented by a null terminated character array.

So to be able to add a character to a string, you must have an array of the correct dimension. BTW, a litteral like "hello" is a const character array: the programmer cannot change anything to it.

So here you want to be able to add 1 to 10 character to "hello", so you need a 16 character array: 5 for hello, 10 for the additional o and 1 for the null. But that is not all, array is just an array of pointers. If all elements of array point to the same string to which you add characters, at the end they will all have the very same value! You must have each element of the array to point to a different character array. Assuming that strdup is available you could write:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = strdup(string);  // build a dynamic copy
}

When you will no longer need array you should free all strings allocated with strdup:

for (int i = 0; i < 10; i++) {
    free(array[i]);
}

BTW, strdup is only in the standard C library since C23, it previously was just a POSIX extension. If is in not available on your system you can roll you own:

char *strdup(const char *src) {
    char *dest = malloc(1 + strlen(src));
    strcpy(dest, src);
    return dest;
}

Alternatively you could avoid dynamic allocation by using a true 2D array:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size][16];      // this is an array of 10 strings of size 16

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    strcpy(array[i], string);
}
生生漫 2025-01-20 05:17:12

除非字符串是动态分配的,否则需要预先确定大小。另外,char str_name[size] 是字符串的定义方式。

int size = 10;
char string[size + 5] = "hello";
char c[2] = "o";
char* array[size];

for (int i = 0; i < size; i++) {
    array[i] = string;
    strcat(string, c);
}

Unless the string is dynamically allocated, the size needs to be pre determined. Also, char str_name[size] is how a string is defined.

int size = 10;
char string[size + 5] = "hello";
char c[2] = "o";
char* array[size];

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