Connecting Tech Pros Worldwide Help | Site Map

number of elemnts in an array

Member
 
Join Date: Sep 2008
Posts: 57
#1: Oct 11 '09
Hi,

I have function which return whit en double* array.
I tried this to get the size :

Expand|Select|Wrap|Line Numbers
  1. double  array[100];
  2.  
  3. double* Function()
  4.     {
  5.            // some code
  6.            return array;
  7.         }
  8.  
  9. int main(int argc, char *argv[])
  10.     {
  11.            double *getarray = Function();
  12.            int  size = sizeof(getarray) / sizeof(getarray[0]);
  13.         }
If I print out the "getarray" variable it give some data, but the value of "size" is 0.
Can anyone explain what's wrong?

Thanks

Arepi
YarrOfDoom's Avatar
Expert
 
Join Date: Aug 2007
Location: Belgium
Posts: 1,118
#2: Oct 12 '09

re: number of elemnts in an array


You're dividing the size of a pointer (so probably 1 byte as result) by the size of an element of the array (which is a double, and therefore more than one byte). The result of this would be less than zero, but since it's saved in a variable of the type int, that number is rounded down to zero.
As far as I know, the most common solution to this problem is to pass the length of the array along with the array itself.
Reply