Thread: does C remember the no of elements in the Array??

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    does C remember the no of elements in the Array??

    int a[99];

    there are 99 (int)s memory allocated.

    why we use sizeof( a ) , it can output 198 ?

    why it knows ? where does it store the no of elements?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    sizeof(a) gives you the size of the whole array, which is made up of sizeof(int) * 99.
    It doesn't store the number of elements, but you can work it out with some simple maths:
    sizeof(a) / sizeof(a[0])

    But be careful if you pass "a" to a function, 'cos sizeof might not do as you expect.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    O~~ thx..

    my Eng. is poor..

    i want to ask how C knows that the no. of element is 99 ??

    and ,, can we get 99 in other method of sizeof(a)/sizeof(a[0]) ??

    just want to know..

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
     
     int main(void)
     {
       int a[99];
       
       printf ("sizeof(a)					  :%lu\n", sizeof(a));
       printf ("sizeof(a[0])				   :%lu\n", sizeof(a[0]));
       printf ("sizeof(int) (same as previous) :%lu\n", sizeof(int));  
       printf ("sizeof(a) / sizeof(a[0])	   :%lu\n", sizeof(a) / sizeof(a[0]));
       printf ("396 / 4					    :%lu\n", 396 / 4);
       
       return(0);
     }
     
     /*
     
      My Output:
      
      sizeof(a)					  :396
      sizeof(a[0])				   :4  
      sizeof(int) (same as previous) :4  
      sizeof(a) / sizeof(a[0])	   :99 
      396 / 4					    :99
      
      */
    (excuse the poor layout, my browser seems to be doing strange things!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    the size of the type INT in your computer is 2 bytes. So the size an array of 99 elements , 2 bytes for each element , is 198.

    and ,, can we get 99 in other method of sizeof(a)/sizeof(a[0]) ??
    yes , try this :
    Code:
    sizeof(a) / 2
    whereas 2 represent the size of int in your computer. Or more accurate\portable code :
    Code:
    sizeof(a) / sizeof(int)

    hammer used "sizeof(a) / sizeof(a[0])" where 'sizeof(a[0])' means the size of one element (the 0 can be replaced with any number within the array range), and since its an INT array , the sizeof(a[0]) would return 2 in your computer and 4 in hammer's , because an INT is either 2 or 4 bytes big (depends on how the computer treats it).

    hope this clears things up
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But again, as Hammer stated, this method will not work unless you are in the original declaring scope of the array:
    Code:
    void foo( int bar[99] )
    {
        printf("The size of the array in foo: %d\n", sizeof( bar ) / sizeof( bar[0] ) );
    }
    
    int main( void )
    {
        int baz[99];
        printf("The size of the array in declared scope: %d\n",
            sizeof( baz ) / sizeof( baz[0] ) );
    
        foo( baz );
    
        return 0;
    }
    Thus, you can only get the size of an array in the scope in which it was declared.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    char a[99];


    sizeof(a); // <<--- How does "sizeof" work to know the no. of elements is 99 ?? I am wondering if there is a place in the memory to store the 99 ??

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    sizeof is a compile time operation. So when the compiler sees
    Code:
    char a[99];
    it adds a to its symbol table and keeps track of it's type and its size.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    OK , i understood now. thank .'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM