Maker Pro
Maker Pro

confuse on Size of array in c program

I am confuse on size of array declaration

Code:
#include<stdio.h>
int main (void)
{
  int i, array[4], size;
   
  /* print size of array on screen */
   printf ("size of array : ");
  /*get size of array from user */
  scanf  ("%d", &size);
   
  for (i = 0; i < size; i++)
  {
    /* print the element of array */
    printf("Enter element of array : " );
     
    /* get array element from user and them stored into array */
    scanf("%d", &array[i]);
  }
    
  return 0;
}
result

size of array : 8
Enter element of array : 1
Enter element of array : 2
Enter element of array : 3
Enter element of array : 4
Enter element of array : 5
Enter element of array : 6
Enter element of array : 7

I don't understand what will happen if I change array size like array[6] or array[8].

array[6] store 6 elements

array[8] store 8 elements

array[2] store 2 element s
 

Harald Kapp

Moderator
Moderator
You can't change the size of an arry in C on the fly (while the program is running). The size of the array is fixed by the definition, here
int array[4].
To use memory of variable size you use dynamic memory allocation, see e.g. here.
result

size of array : 8
This will not work correctly. Your array has only 4 elements (defined by array[4]. When you access elements outside the defined index range (array[0]...array[3]) you access some memory location that is used for other purposes. It may e.g. hold other variables which you overwrite by your code. It is good practice to check array accesses to be within the bounds of the array, i.e.index >= 0 and index < max. index defined for teh array. The sizeof operator can be used for this purpose.

I don't understand what will happen if I change array size like array[6] or array[8].
array[6] store 6 elements

array[8] store 8 elements

array[2] store 2 element s
I do not understand where's the problem. It is as you describe.
 
I do not understand where's the problem. It is as you describe.
If you look in the program, you will see two array sizes, one is array[4] and other is size of array : (8 user define)

How will you decide what could the size array[number] ? What changes happen if I change number like 2,4,6,8,9.. in given program ? will it still store only 8 numbers ?
 

Harald Kapp

Moderator
Moderator
If you look in the program, you will see two array sizes, one is array[4] and other is size of array : (8 user define)
No.
There is only one definition in line 4:
Code:
int i, array[4], size;
Entering a size bigger than that defined will lead to an error as described in my post #2. The value of the variable 'size' needs to be not higher than the real size of the array (4 integers).
This may seemingly work in your small example, but there is no guaranteee it will work in the context of a larger program, another compiler etc.

Addendum: A C compiler will not prevent you from addressing an array element outside of the defined range. An array element is adressed by converting the index in square brackets into a memory address:
address = address_of_array[0] + index*size_of_array_element
Example:
Assume the array is located at address 0x0000.
Assume array is defined as
int16 array[4];
The element size is int16 which requires 2 bytes.
array[0] is then located at 0x0000 (byte addresses 0x0000 and 0x0001 are used for storing 1 int16 value)
array[1] is then located at 0x0002
...
array[3] is located at 0x0006

When you try to access array[4], the processor will calculate an address of 0x0008 which in this case is a valid memory address. However, this address is not reserved for contents of the array. The compiler may have assigned this address to e.g. the variable 'size', since it only reserved the range 0x0000...0x0007 (4 * int16) for this array. When you write to array[4] this will overwrite any data present at this address. In my example the value of the variable 'size' may be changed.

Read here for a more detailed explanation.
 
Last edited:
Top