Thread: size of array declaration

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    size of array declaration

    C program

    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 am confuse on size of array declaration like array[4] that means the array is capable to only four elements but if I enter size of array it store number 1 - 7. why it does not store only four numbers

    if I don't give any number then its show error
    hello.c: In function 'main':
    hello.c:4:10: error: array size missing in 'array'
    int i, array[], size;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > why it does not store only four numbers
    Because C has no built in bounds checking.

    C99 allows you to do this.
    Code:
    scanf  ("%d", &size);
    int array [size];
    Otherwise, you have to use malloc and free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    why it does not store only four numbers
    Because C has no built in bounds checking.
    I don't understand what will happen if value array[4] change in program. what happen if it will be array[6] or array[8]. I don't understand basic difference

    array[6] store 6 elements

    array[8] store 8 elements

    array[2] store 2 element s

    but I use array[4] in my program so it should be store 4 elements but it store more then 4.

    I have decleared maximum size of array is 4 but if user enter array size more then the 4 then program store element more then 4

    Last edited by vead; 01-02-2018 at 04:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Declaration
    By zachsformacs95 in forum C Programming
    Replies: 10
    Last Post: 04-20-2010, 10:23 PM
  2. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  3. Array declaration
    By freevryheid in forum C Programming
    Replies: 5
    Last Post: 01-12-2009, 10:53 PM
  4. problem with matrix size declaration
    By angelica in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2008, 09:03 AM
  5. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM

Tags for this Thread