Thread: What is size of array

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

    What is size of array

    What would be size of int a[4];

    Code:
    #include<stdio.h>int main ()
    {
    	int a[4];
        int n = sizeof(a);
    	printf (" Size of array %d ", n);
    	return 0;
    }
    integer take 4 byte in my machine so each element take 4 byte to store data. This array can store five element a[0] a[1] a[2] a[3] a[4]

    I think the size of array should be 4 *5 = 20 bytes but program show size of array is 16 bytes

    What would be size of int a[4];

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    No, this array can store four elements, a[0], a[1], a[2] and a[3]. When you say "int a[4];", you're asking for an integer array with 4 elements, not with a maximum index of 4.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by GReaper View Post
    No, this array can store four elements, a[0], a[1], a[2] and a[3]. When you say "int a[4];", you're asking for an integer array with 4 elements, not with a maximum index of 4.
    What happen in this case when int a[4];

    How much size array take 16 bytes because int take one byte to one one integer value so this array will store 4 integer that mans it will take 16 bytes

    What happen in this case when
    Code:
     int a[4] = {1,2,3,4};


    I think it also take 16 bytes

    what would be the size of array for
    Code:
     int a[4] = {1,2,3,4}


    what would be the size of array for int a[4];
    what would be the size of array for int a[4] = {1,2,3,4};

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    4 bytes times 4 variables is 16. This is basic arithmetic you are confused about.

    int a[5]; would be 20 bytes for the same reasoning, but we are talking about int a[4];

    Please remember that the highest subscript (or index) is one less than the declared size of the dimension, such that int a[4] has a high subscript of 3, int a[5] has a high subscript of 4, and so on and so forth.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    4 bytes times 4 variables is 16. This is basic arithmetic you are confused about..
    I am confused on two line's what happen when they run on machine

    1.What happen when this line execute a[4]; ?
    what would be the size of array for int a[4]; i think it would be 16

    2.What happen when this line execute int a[4] = {1,2,3,4}; ?

    what would be the size of array for int a[4] = {1,2,3,4}; i think it would be 16

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    What you seem to be asking is would the size of an uninitialized array be different from the size of an initialized one. Try this program, and see what it prints out:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int a[4];
        int b[4] = {1, 2, 3, 4};
        printf("sizeof(a) = %zd\n", sizeof(a));
        printf("sizeof(b) = %zd\n", sizeof(b));
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by anduril462 View Post
    What you seem to be asking is would the size of an uninitialized array be different from the size of an initialized one. Try this program, and see what it prints out:
    Both initialized and uninitialized aray print the same value zd. Working size for both will same

  8. #8
    Registered User
    Join Date
    Feb 2018
    Posts
    6
    Hey there,
    The thing with arrays is that the moment you type something like int[4] a; what the compiler really does is that it just does the initialization itself by the default value of the given data type of that array if it is a primitive type. So, in this case it becomes something like
    Code:
     int[4] a={0,0,0,0}
    IMPLICITLY. but when you choose to initialize the array yourself then you just write your values in the curly braces. But the size remains the same in both the cases.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    so when we don't initialize array it will store garbage value

    Code:
    #include<stdio.h>int main(){
    	
    int array[4];
    
    
    printf("array element     : %d \n", array[0]);
    printf("addaress of array : %d \n", &array[0]);
    printf("array element     : %d \n", array[1]);
    printf("addaress of array : %d \n", &array[1]);
    printf("array element     : %d \n", array[2]);
    printf("addaress of array : %d \n", &array[2]);
    printf("array element     : %d \n", array[3]);
    printf("addaress of array : %d \n", &array[3]);
    printf("array element     : %d \n", array[4]);
    printf("addaress of array : %d \n", &array[4]);
    	return 0;
    }
    array element : 4200912
    addaress of array : 6422304
    array element : 0
    addaress of array : 6422308
    array element : 3141632
    addaress of array : 6422312
    array element : 4194432
    addaress of array : 6422316
    array element : 6422300
    addaress of array : 6422320

    When initialize array

    Code:
    #include<stdio.h>
    int main(){
    	
    int array[4] = {4,2,3,5};
    
    
    printf("array element     : %d \n", array[0]);
    printf("addaress of array : %d \n", &array[0]);
    printf("array element     : %d \n", array[1]);
    printf("addaress of array : %d \n", &array[1]);
    printf("array element     : %d \n", array[2]);
    printf("addaress of array : %d \n", &array[2]);
    printf("array element     : %d \n", array[3]);
    printf("addaress of array : %d \n", &array[3]);
    
    
    
    
    	return 0;
    }
    array element : 4
    addaress of array : 6422304
    array element : 2
    addaress of array : 6422308
    array element : 3
    addaress of array : 6422312
    array element : 5
    addaress of array : 6422316

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by vishaljamdagni View Post
    Hey there,
    The thing with arrays is that the moment you type something like int[4] a; what the compiler really does is that it just does the initialization itself by the default value of the given data type of that array if it is a primitive type. So, in this case it becomes something like
    Code:
     int[4] a={0,0,0,0}
    IMPLICITLY. but when you choose to initialize the array yourself then you just write your values in the curly braces. But the size remains the same in both the cases.
    No! If it's not global and it's not static then there is NO implicit initialisation; there is no initialisation at all

    Quote Originally Posted by vead View Post
    so when we don't initialize array it will store garbage value
    That's correct because unless you initialise the values to something they remain uninitialised; i.e. garbage as you call it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  2. How do I get the size of an array from a pointer to the array?
    By Programmer_P in forum C++ Programming
    Replies: 31
    Last Post: 06-03-2010, 04:13 PM
  3. 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
  4. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  5. size of array
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-19-2002, 10:26 AM

Tags for this Thread