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

后端 未结 6 521
轻奢々
轻奢々 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:18

    You can have a counter (freePosition), which will track the next free place in an array of size n.

    0 讨论(0)
  • 2021-02-05 04:20
    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.

    Also keep in mind that arr starts at index arr[0] and ends at index arr[9] - 10 elements

    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

    arr[4] = 5;
    

    you allocate the value 5 to the fifth element of the array.

    You could do this until you allocate values for the last index of the arr that is arr[9];

    Sorry if my explanation is choppy, but I have never been good at explaining things.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 04:35

    Short answer is: You don't have any choice other than:

    arr[4] = 5;
    
    0 讨论(0)
  • 2021-02-05 04:36

    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.

    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 8 to 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.

    0 讨论(0)
  • 2021-02-05 04:41

    For some people which might still see this question, there is another way on how to append another array element(s) in C. You can refer to this blog which shows a C code on how to append another element in your array.

    But you can also use memcpy() function, to append element(s) of another array. You can use memcpy()like this:

    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    
    int first_array[10] = {45, 2, 48, 3, 6};
    int scnd_array[] = {8, 14, 69, 23, 5};
    
    // 5 is the number of the elements which are going to be appended
    memcpy(a + 5, b, 5 * sizeof(int));
    
    // loop through and print all the array
    for (i = 0; i < 10; i++) {
        printf("%d\n", a[i]);
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题