-3

I want to enter n number of elements to an array from the user but it does not take an input after 1 element and prints only that element and terminates and does not print the whole array.dont know why it is not working even though there isn't any error.

#include <stdio.h>

int main()
{
    int arr[100] = {1, 2, 3, 4, 5};
    int n, q, m = sizeof(arr) / sizeof(arr[0]);
    printf("enter the number of elements to be added:");
    scanf("%d", &n);
    q = m + n;
    for (int i = m; i < q; i++)
    {
        printf("enter the number:");
        scanf("%d", &arr[i]);
    }
    for (int i = 0; i < q; i++)
    {
        printf("%d\t", arr[i]);
    }
    return 0;
}

this is how it appears at the terminal:

enter the number of elements to be added:3

enter the number:1

1

PS C:\Users\new user\code\c tutorials>

  • 3
    Your for loop breaches your array right out of the gate. It starts with `m` being 100. Run your code in a debugger, step to the loop start, and you'll see that. – WhozCraig Oct 25 '22 at 12:15
  • 3
    Did you look at `m`? It will be 100. The `sizeof` operator doesn't care what values you place in an array. So you're indexing past the end of your array. – Tom Karzes Oct 25 '22 at 12:15
  • 2
    Start from giving variables meaningful names, the everything will become obvious. – 0___________ Oct 25 '22 at 12:16
  • 1
    @0___________ I disagree, WhozCraigs comment on the sizeof on a partially explicitly initialised array is more to the point. I suspect a deeper misunderstanding that will not be fixed by reflecting the (misunderstood) semantics of variables in their names. – Yunnosch Oct 25 '22 at 12:22
  • `sizeof(arr) / sizeof(arr[0])` is the total number of elements of the array (it's 100). The number of meaningful values in the array is 5 and that's what you should put into `m`. – Jabberwocky Oct 25 '22 at 12:36
  • 1
    You need to learn basic debugging yourself. Asking others to tell values of your variables from the code, when you could just use a debugger or simply print them ... shows zero research effort in your part. – hyde Oct 25 '22 at 12:43
  • C arrays do not have size known at runtime. You need a separate variable to keep track of elements in use. I recommend using a struct to combine the array (or pointer to allocation), maximum capacity and current used count. – hyde Oct 25 '22 at 12:46

1 Answers1

1

You cant add or remove elements from the array in C language (ie you cant change its size).

You can only increase the amount of dynamically allocated memory

int *add(int *arr, const int val, size_t size)
{
    arr = realloc(arr, (size + 1) * sizeof(*arr));
    if(arr)
    {
        arr[size] = val;
    }
    return arr;
}


int main(void)
{
    int  *array = NULL;
    size_t size = 0;

    size += !!(array = add(array, 1, size));
    size += !!(array = add(array, 2, size));
    size += !!(array = add(array, 3, size));

    printf("Array size is: %zu\nElements:\n", size);
    for(size_t i = 0; i < size; i ++)
    {
        printf("[%zu]\t%d\n", i, array[i]);
    }
    free(array); 
}

or more correct:

int main(void)
{
    int  *array = NULL, *tmp;
    size_t size = 0;

    size += !!(array = (tmp = add(array, 1, size)) ? tmp : array);
    size += !!(array = (tmp = add(array, 2, size)) ? tmp : array);
    size += !!(array = (tmp = add(array, 3, size)) ? tmp : array);
    size += !!(array = (tmp = add(array, 4, size)) ? tmp : array);
    size += !!(array = (tmp = add(array, 5, size)) ? tmp : array);

    printf("Array size is: %zu\nElements:\n", size);
    for(size_t i = 0; i < size; i ++)
    {
        printf("[%zu]\t%d\n", i, array[i]);
    }
    free(array); 
}

https://godbolt.org/z/Gb59or4eM

0___________
  • 60,014
  • 4
  • 34
  • 74
  • You may want to change how `realloc` is called in this snippet. See e.g. https://stackoverflow.com/a/1986930/4944425 – Bob__ Oct 25 '22 at 13:24
  • @Bob__ no, it is correct as it changes only local variable arr. It is 100% fine and correct. That link is a "rule of thumb" not needed in this case. Caller should check for the result. See edited version – 0___________ Oct 25 '22 at 14:17