Can someone explain how to append an element to an array in C programming?

后端 未结 6 520
轻奢々
轻奢々 2021-02-05 03:55

If I want to append a number to an array initialized to int, how can I do that?

int arr[10] = {0, 5, 3, 64};
arr[] += 5; //Is this it?, it\'s not working for me.         


        
6条回答
  •  北海茫月
    2021-02-05 04:35

    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.

提交回复
热议问题