C语言 有人可以解释如何在 C 编程中将元素附加到数组吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26208788/
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
Can someone explain how to append an element to an array in C programming?
提问by user3326078
If I want to append a number to an array initialized to int, how can I do that?
如果我想将一个数字附加到初始化为 int 的数组中,我该怎么做?
int arr[10] = {0, 5, 3, 64};
arr[] += 5; //Is this it?, it's not working for me...
I want {0,5, 3, 64, 5} in the end.
最后我想要 {0,5, 3, 64, 5} 。
I'm used to Python, and in Python there is a function called list.append that appends an element to the list automatically for you. Does such function exist in C?
我已经习惯了 Python,在 Python 中有一个名为 list.append 的函数,它会自动为您添加一个元素到列表中。C中是否存在这样的功能?
采纳答案by bitcell
int arr[10] = {0, 5, 3, 64};
arr[4] = 5;
EDIT:So I was asked to explain what's happening when you do:
编辑:所以我被要求解释当你这样做时发生了什么:
int arr[10] = {0, 5, 3, 64};
you create an array with 10 elements and you allocate values for the first 4 elements of the array.
您创建一个包含 10 个元素的数组,并为数组的前 4 个元素分配值。
Also keep in mind that arrstarts at index arr[0]and ends at index arr[9]- 10 elements
还要记住, arr从索引开始到索引arr[0]结束arr[9]- 10 个元素
arr[0] has value 0;
arr[1] has value 5;
arr[2] has value 3;
arr[3] has value 64;
after that the array contains garbage values / zeroes because you didn't allocated any other values
之后数组包含垃圾值/零,因为您没有分配任何其他值
But you could still allocate 6 more values so when you do
但是你仍然可以再分配 6 个值,所以当你这样做时
arr[4] = 5;
you allocate the value 5 to the fifth element of the array.
您将值 5 分配给数组的第五个元素。
You could do this until you allocate values for the last index of the arrthat is arr[9];
直到你的最后的指数值分配你可以这样做arr就是arr[9];
Sorry if my explanation is choppy, but I have never been good at explaining things.
对不起,如果我的解释断断续续,但我从来不擅长解释事情。
回答by Arjun Prasad
You can have a counter (freePosition), which will track the next free place in an array of size n.
您可以有一个计数器 (freePosition),它将跟踪大小为 n 的数组中的下一个空闲位置。
回答by Amadan
There are only two ways to put a value into an array, and one is just syntactic sugar for the other:
将值放入数组只有两种方法,一种只是另一种的语法糖:
a[i] = v;
*(a+i) = v;
Thus, to put something as the 4th element, you don't have any choice but arr[4] = 5. However, it should fail in your code, because the array is only allocated for 4 elements.
因此,要将某些东西作为第 4 个元素,您别无选择,只能arr[4] = 5. 但是,它应该在您的代码中失败,因为该数组仅分配给 4 个元素。
回答by Vignesh kanna
If you have a code like
int arr[10] = {0, 5, 3, 64};, and you want to append or add a value to next index, you can simply add it by typing a[5] = 5.
如果您有一个类似 的代码
int arr[10] = {0, 5, 3, 64};,并且您想在下一个索引中附加或添加一个值,您只需键入 即可添加它a[5] = 5。
The main advantage of doing it like this is you can add or append a value to an any index not required to be continued one, like if I want to append the value 8to index 9, I can do it by the above concept prior to filling up before indices.
But in python by using list.append()you can do it by continued indices.
这样做的主要优点是您可以向不需要继续的任何索引添加或附加值,例如如果我想将值附加8到索引 9,我可以在填充之前通过上述概念来完成在指数之前上升。但是在 python 中,list.append()你可以通过使用连续索引来做到这一点。
回答by Sherzod
Short answer is:You don't have any choice other than:
简短的回答是:除了:
arr[4] = 5;

![C语言 char * 和 char[] 的区别](/res/img/loading.gif)