sizeof array

This is a discussion on sizeof array within the C Programming forums, part of the General Programming Boards category; Hi, Was trying this program Code: #include<stdio.h> int retsize( int a[] ); int main() { int arr[]={1,2,3,4}; printf(" size = ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Question sizeof array

    Hi,
    Was trying this program

    Code:
    #include<stdio.h>
    
    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

    Thanks
    MAx

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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, 08:51 AM
  2. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 01: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, 12:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21