0

I am trying to implement insertion of an element in an array on my computer but size_array=sizeof(array)/sizeof(array[0]) was giving the wrong answer, the array contains 5 elements but this piece of code was printing size equal to 2.

#include <stdio.h>
#include <stdlib.h>

void insert(int * array,int number_of_elements,int index,int value)
{
    int size_array,i;

    size_array=sizeof(array)/sizeof(array[0]);
    printf("%d\n",size_array);

    if(size_array==number_of_elements)
    {
        printf("Insertion failed");
        exit;
    }
    else
    {
        for(i=number_of_elements;i>index;i--)
        {
           array[i]=array[i-1];
        }
        array[index]=value;
    }
}

int main()
{
    int array[5],i,number_of_elements=5,value=-9,index=3;

    for(i=0;i<=number_of_elements-1;i++)
    {
        scanf("%d",&array[i]);
    }
    printf("\n");
    for(i=0;i<=number_of_elements-1;i++)
    {
        printf("%d\t",array[i]);
    }
    printf("\n");

    insert(array,number_of_elements,index,value);

    for(i=0;i<=number_of_elements;i++)
    {
        printf("%d\t",array[i]);
    }
    printf("\n");
    return 0;
}

  • At the location where you use it, `sizeof(array)` is the size of `int *` – bereal Sep 27 '21 at 07:59
  • Because pointer isn't an array – qrdl Sep 27 '21 at 08:00
  • `sizeof(array)` is the size of the pointer, not the size of the array. – Andreas Wenzel Sep 27 '21 at 08:00
  • @qrdl What should I do to correct it? – Shreyansh Pathak Sep 27 '21 at 08:01
  • 1
    @ShreyanshPathak you need to pass array size as function param – qrdl Sep 27 '21 at 08:02
  • 1
    Side note: Instead of writing `for(i=0;i<=number_of_elements-1;i++)`, it is easier and more common to write `for(i=0;i – Andreas Wenzel Sep 27 '21 at 08:03
  • @qrdl So it means I can't find the size of an array using pointer to it? – Shreyansh Pathak Sep 27 '21 at 08:05
  • You are already passing the number of elements in the array to the function, in the `number_of_elements` argument. Why are you attempting to calculate the number of elements again using `size_array=sizeof(array)/sizeof(array[0]);`? – Andreas Wenzel Sep 27 '21 at 08:10
  • @ShreyanshPathak: No, there is no way to obtain the size of an array from a pointer to the start of the array. You must keep track of that information by some other means. – Andreas Wenzel Sep 27 '21 at 08:11
  • This loop will cause you to access `array` out of bounds: `for(i=0;i<=number_of_elements;i++)`. Note that in C, arrays are fixed size and cannot be increased at runtime. – Andreas Wenzel Sep 27 '21 at 08:15
  • @AndreasWenzel I was calculating `size_array` because, if number_of_elements and size_array come out to be equal then I shouldn't be able to insert an element in the array, please guide me if I am wrong somewhere. – Shreyansh Pathak Sep 27 '21 at 08:15
  • Is `number_of_elements` supposed to represent the number of valid elements inside the array, or the maximum size of the array? – Andreas Wenzel Sep 27 '21 at 08:19
  • @AndreasWenzel `number_of_elements` represent the number of elements which I want to insert in the array using scanf , it is not the maximum size of the array . – Shreyansh Pathak Sep 27 '21 at 08:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237547/discussion-between-andreas-wenzel-and-shreyansh-pathak). – Andreas Wenzel Sep 27 '21 at 08:24
  • A pointer points to a *single* object - that object *may* be the first element of an array, but there’s no way to know that based on the pointer value alone. You will have to keep track of the array size and pass that size as a separate parameter to the function. – John Bode Sep 27 '21 at 12:46

0 Answers0