Thread: size of array - why function gives size ONE only

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    size of array - why function gives size ONE only

    the user-defined function size_of_array(numA) is meant to give the size of numA which is 5 but it gives 1... I wonder where went wrong? can anyone help pls...
    insert
    Code:
    #include <iostream>
    using namespace std;
    int size_of_array(int numA[]);
    
    int main()
    {
    	int numA[]={0,1,2,3,4};
    	int i=0;
    	int total=0;
    
    	for(i=0; i<size_of_array(numA); i++)
    	{
    		total+=numA[i];
    	}
    	float average=total/i;
    	cout << "average is "<<average;
    	
    	return 0;
    }
    int size_of_array(int numA[])
    {
    		int num_of_elements=sizeof(numA)/sizeof(int);
    		return num_of_elements;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This has been answered elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Dawnson's Avatar
    Join Date
    Oct 2009
    Location
    In front of my computer
    Posts
    6
    Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dawnson View Post
    Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1.
    Wrong. sizeof(numA) is 4 here. Had numA been an array rather than a pointer, of 5 elements, sizeof(numA) would have been 20.
    Yes, depending on the compiler.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Dawnson View Post
    Obviously, sizeof(numA) is 5, and sizeof(int) is 4(depending on the compiler). You have made 5/4 and that results in 1.
    Absolutely wrong. If num[] would have contained 8 elements then according to you the answer should have been 2 which is incorrect. The correct reason is in the link given by laserlight. When you pass an array to a function it decomposes into pointer to the the first element and thus sizeof(num)==4 which is equal to sizeof(int).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You could do this instead.
    Code:
    #include <iostream>
    using namespace std;
    
    template<typename T, int N>
    char (&array(T (&)[N]))[N];
    
    int main()
    {
    	int numA[]={0,1,2,3,4};
    	int i=0;
    	int total=0;
    
    	for(i=0; i<sizeof(array(numA)); i++)
    	{
    		total+=numA[i];
    	}
    	float average=total/i;
    	cout << "average is "<<average;
    	
    	return 0;
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or how about using some typedefs? I am having trouble discerning the types that goes into that function. It's pure luck I can see it's a function, too.
    Code:
    template<typename T, int N>
    void myfunction(T (& name)[N])
    {
        //...
    }
    But I also believe something like this works, where N is the number of elements. You could do a sizeof on name and get the real size in this case.
    Or you could just use a vector and use the .size() method and multiple with the size of one element.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I think we need to wait til C++0x in order to really simplify that ugly function signature
    Code:
    template<typename T, int N> 
    typedef T (&array_t)[N];
    ** I assume that this will eventually be legal, although even Comeau with C++0x extensions doesn't recognise it right now, so perhaps I misunderstand what they actually mean by "adding support for template typedefs" :-)


    Yes I would prefer an STL container too; Personally I only find myself using these to initialise containers using const arrays as lookup tables.

    The reason I prefer the sizeof(char(&)[N]) approach is that the return type can be evaluated at compile time without ever actually calling the function, but I suppose a solution which actually calls a function would be OK aswell.
    Code:
    template<typename T, int N>
    int array_size( T (&dummy_arg)[N] )
    {
        return N;
    }
    Or, if a range is needed for an STL container or algorithm then maybe returning a pointer would be better
    Code:
    template<typename T, int N>
    T* end_of( T (&array)[N] )
    {
        return array+N;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM