Thread: sizeof array

  1. #1
    Registered User
    Join Date
    Jul 2008

    Question sizeof array

    Was trying this program

    int retsize( int a[] );
    int main()
         int arr[]={1,2,3,4};
         printf(" size = %d \n",sizeof(arr));
         printf(" size = %d \n",retsize(arr));
         return 0;
    int retsize (int arr[])
        return sizeof(arr);
    The answer im getting is
    size = 16
    size = 4

    i wonder why the function is not returning 16 too.
    please shed some light. and how do i correct it


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    It is not possible to pass an array to a function. Instead, a pointer to the first element is passed (and as you see, pointers are four bytes).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    And if you actually want to use the size of an array when it has been passed to a function, you need to pass the original size from the calling code - that is, add another parameter to the function that indicates the size.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 02:40 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM