I tried to create an Array type ADT.
// Array ADT using structure
struct ARRAY_ADT
{
int *A; // For storing the pointer to base address of the array
int size; //For storing the size of the array
int len; //For storing the length of the array
};
when I initialize this struct instance call it Array using a function called init.
struct ARRAY_ADT init()
{
struct ARRAY_ADT Array;
printf("Enter the size of the Array: ");
Array.size = Integerinput();
//Creating the pointer for the Array;
Array.A = (int*) malloc(Array.size * sizeof(int));
//printf("Size of from init: %d", sizeof(Array.A));
do
{
printf("Enter the length of the Array: ");
Array.len = Integerinput();
}while(OutofRange(Array.size,Array.len));
printf("Please enter the values in the array: ");
for(int i = 0; i < Array.len; i++)
{
Array.A[i] = Integerinput();
}
return Array;
}
Now, theoretically I can only add 4 integers if size is 4 as I have only allocated 4 memory location using malloc but in this case I can add as much number as I want to add in the Array ADT. I had commented the OutofRange function for sake of testing purpose. Even, It's not losing or overwriting any number I have checked for it too.
So, what is happening here? Why it's showing such type of behaviour?
Is there any way to correct or resolve this type of issue rather than relying on your own OutofRange function?