Thread: finding size of empty char array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by prog-bman
    This will do:
    Code:
    sizeof(array) / sizeof(array[0])
    No it won't. Read the OP.
    Now I pass the array to a function:

    function(myarray);

    How can I find out inside the function the size allocated to the array?
    Code:
    #include<stdio.h>
    
    void foo( int array[] )
    {
        printf( "sizeof( array ) is %d\n", sizeof( array ) );
        printf( "sizeof( array[ 0 ] ) is %d\n", sizeof( array[ 0 ] ) );
        printf( "sizeof( array ) / sizeof( array[ 0 ] ) is %d\n",
            sizeof( array ) / sizeof( array[ 0 ] ) );
    }
    
    void bar( char array[] )
    {
        printf( "sizeof( array ) is %d\n", sizeof( array ) );
        printf( "sizeof( array[ 0 ] ) is %d\n", sizeof( array[ 0 ] ) );
        printf( "sizeof( array ) / sizeof( array[ 0 ] ) is %d\n",
            sizeof( array ) / sizeof( array[ 0 ] ) );
    }
    
    int main( void )
    {
        int array[ 10 ];
        char barray[ 10 ];
    
        foo( array );
        bar( barray );
    
        return 0;
    }
    
    /*
    sizeof( array ) is 4
    sizeof( array[ 0 ] ) is 4
    sizeof( array ) / sizeof( array[ 0 ] ) is 1
    sizeof( array ) is 4
    sizeof( array[ 0 ] ) is 1
    sizeof( array ) / sizeof( array[ 0 ] ) is 4
    */
    Arrays degrade to pointers to their first element when passed to functions. No cigar for you.


    Quzah.
    Last edited by quzah; 05-28-2006 at 11:24 PM. Reason: Added bar.
    Hope is the first step on the road to disappointment.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    57
    I don't want to pass the size of array as arguement because I want the function to do error checking on the passed array, to be certain that it doesn't overwrite array boundries. The size of array n is undetermined before compile time and needs to be known.

    function(char * p)

    function(char p[])

    The problem with sizeof here is that it probably measures the size of the pointer. At any rate sizeof(p) and strlen always return 4, regardless of the actual size of the array.

    The size of the array must be stored somewhere, and a function recieving the array as an arguement ought to be able to access it.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>The size of the array must be stored somewhere

    No it doesn't - arrays do not have built in bounds checking

    >>The size of array n is undetermined before compile time

    So you are using a dynamic memory allocation i guess - how
    does that know/calculate the size of the array?

    Just pass it as a parameter. I suppose your function could read
    the array until the string termination character - but does your
    program guarantee that the char array passed is an actual
    string?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM