C语言 将元素附加到 C 中的字符串数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29699451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:52:03  来源:igfitidea点击:

Appending element into an array of strings in C

carraysappend

提问by dave_1234

I have an array of strings with a given size, without using any memory allocation, how do I append something into it?

我有一个给定大小的字符串数组,不使用任何内存分配,我如何在其中添加一些内容?

Say I run the code, its waiting for something you want to enter, you enter "bond", how do I append this into an array ? A[10] ?

假设我运行代码,它在等待你想要输入的东西,你输入"bond",我如何将它附加到一个数组中?[10] ?

采纳答案by Vlad from Moscow

If the array declared like

如果数组声明为

char A[10];

then you can assign string "bond" to it the following way

那么您可以通过以下方式为其分配字符串“bond”

#include <string.h>

//...

strcpy( A, "bond" );

If you want to append the array with some other string then you can write

如果你想用其他字符串追加数组,那么你可以写

#include <string.h>

//...

strcpy( A, "bond" );
strcat( A, " john" );

回答by Nick Bailey

You can't append to an array. When you define the array variable, C asks the is for enough contiguous memory. That's all the memory you ever get. You can modify the elements of the array (A[10]=5) but not the size.

您不能附加到数组。当您定义数组变量时,C 会询问是否有足够的连续内存。这就是你得到的所有记忆。您可以修改数组的元素 (A[10]=5) 但不能修改大小。

However, you CAN create data structures that allow appending. The two most common are linked lists and dynamic arrays. Note, these are no built into the language. You have to implement them yourself or use a library. The lists and arrays of Python, Ruby and JavaScript are implemented as dynamic arrays.

但是,您可以创建允许附加的数据结构。最常见的两种是链表和动态数组。请注意,这些不是内置于语言中的。您必须自己实现它们或使用库。Python、Ruby 和 JavaScript 的列表和数组是作为动态数组实现的。

LearnCThHardWay has a pretty good tutorial on linked lists, though the one on dynamic arrays is a little rough.

LearnCThHardWay 有一个关于链表的非常好的教程,尽管关于动态数组的教程有点粗糙。

回答by Naliwe

Hi,

你好,

It really depends on what you mean by append.

这真的取决于你所说的附加是什么意思。

...
int tab[5]; // Your tab, with given size
// Fill the tab, however suits you.
// You then realize at some point you needed more room in the array
tab[6] = 5; // You CAN'T do that, obviously. Memory is not allocated.

The problem here can be two things :

这里的问题可能是两件事:

  • Did you misjudge the size you need ? In that case, just make sure this given size you mentioned is correctly 'given', however that might be.
  • Or don't you know how much room you want at the beginning ? In that case, you''ll have to allocate the memory yourself ! There is no other way you can resize a memory chunk on the fly, if I might say.
  • 您是否误判了您需要的尺寸?在这种情况下,只需确保您提到的这个给定尺寸是正确“给定”的,但可能是这样。
  • 或者你一开始不知道你想要多少空间?在这种情况下,您必须自己分配内存!如果我可以说,没有其他方法可以即时调整内存块的大小。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STR_MAX_SIZE 255                                // Maximum size for a string. Completely arbitray.
char *new_string(char *str) { char *ret; // The future new string;
ret = (char *) malloc(sizeof(char) * 255); // Allocate the string strcpy(ret, str); // Function from the C string.h standard library return (ret); }
int main() { char *strings[STR_MAX_SIZE]; // Your array char in[255]; // The current buffer int i = 0, j = 0; // iterators
while (in[0] != 'q') { printf("Hi ! Enter smth :\n"); scanf("%s", in); strings[i] = new_string(in); // Creation of the new string, with call to malloc i++; } for ( ; j < i ; j++) { printf("Tab[ %d ] :\t%s\n", j, strings[j]); // Display free(strings[j]); // Memory released. Important, your program // should free every bit it malloc's before exiting }
return (0); }




这是我能想到的最简单的解决方案。这可能不是最好的,但我只是想向您展示整个过程。我可以使用 C 标准库strdup(char *str)strdup(char *str)函数来创建一个新字符串,并且可以实现我自己的快速列表或数组。

回答by Taylor Liang

If you want to append a character or string to it;

如果你想向它附加一个字符或字符串;

strcpy(a, "james")
strcpy(a, "bond")

回答by fuz

The size of an array variable cannot change. The only way to append to an array is to use memory allocation. You are looking for the realloc()function.

数组变量的大小不能改变。追加到数组的唯一方法是使用内存分配。您正在寻找该realloc()功能。