Thread: finding size of empty char array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    finding size of empty char array

    Lets say I create a char array:
    char myarrya[20];

    Now I pass the array to a function:

    function(myarray);

    How can I find out inside the function the size allocated to the array? Assuming no string is placed in array yet. Strlen() and sizeof() only give the size of a string placed in the array.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Wouldn't sizeof(char)*20 work ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    57
    The size of the array entered into the function will be undetermined until run time:
    myarray[n]
    function(myarray)

    The function needs to know the value of n

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Hmm....then you might want to pass it as a parameter, that's my take on it.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You're sure that you need to know the size of an empty character array? I'm sure that character arrays are capable of being passed without a parameter for the size. You just need to change the function a bit.
    Code:
    function(char myarray[])

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    This will do:
    Code:
    sizeof(array) / sizeof(array[0])
    Woop?

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I'd suggest something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void test( char arr[], int size );
    
    int main(int argc, char *argv[])
    {
        
        char array[20];
        
        test(array, sizeof(array) / sizeof(array[0]) );
        
        system("PAUSE");	
        return 0;
    }
    
    void test( char arr[], int size )
    {
         /* code */
    }

    [EDIT]
    Edited to show elements allocated, instead of memory allocated.
    Last edited by bivhitscar; 05-28-2006 at 09:38 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    That code will only work if the size of a char is only one which I am not sure that is guranteed to be, I think so though. Anyways if you want to use it for more than chars you will have to use my method.
    Woop?

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Ahh yes, I read size as memory space, prog-bman's method will show you how many elements are allocated.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    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.

  11. #11
    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.

  12. #12
    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

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That code will only work if the size of a char is only one which I am not sure that is guranteed to be, I think so though. Anyways if you want to use it for more than chars you will have to use my method.
    That is correct.
    http://gcc.gnu.org/ml/gcc-bugs/1998-09/msg00834.html :
    `sizeof(char) , sizeof(signed char) and sizeof(unsigned char) are 1'
    http://www.ussg.iu.edu/hypermail/lin...01.2/0969.html :
    A7.4.8 Sizeof Operator
    The sizeof operator yields the number of bytes required to store an
    object of the type of its operand. [...] When sizeof is applied to a
    char, the result is 1. [...] The operator may not be applied to [...]
    a bit-field.
    http://www.parashift.com/c++-faq-lit....html#faq-26.1 :
    sizeof(char) is always exactly 1. No exceptions, ever.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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