sizeof() only works as parameter, not as variable in my function?

The following is my code to average the doubles in an array:

#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
long double average(long double n[],long int num_of_terms){
    long double sum=0;
	long int i;
    for(i=0; i<num_of_terms; i++)sum+=n[i];
    return sum/num_of_terms;
}
int main(){
	long double n[]={1,2,3,4,5,6,7};
    cout<<average(n,sizeof(n)/16)<<endl;
}



apparently, I discovered that the sizeof returned 16x the number of elements in the array,

which is y my num_of_terms parameter is sizeof(n)/16

however, if i make sizeof(n)/16 a variable in the average function, it returns

nan every time? What the hell?? pls help...
Last edited on
Try sizeof(n)/sizeof(*n).

however, if i make sizeof(n)/16 a variable in the average function, it returns
How do you mean?
1
2
3
4
5
6
7
8
9
10
11
long double average(long double n[]){
long int num_of_terms=sizeof(n)/16;//no longer a parameter
long double sum=0;
long int i;
for(i=0; i<num_of_terms; i++)sum+=n[i];
return sum/num_of_terms;
}
int main(){
long double n[]={1,2,3,4,5,6,7};
cout<<average(n)<<endl;
}




this returns nan
why?? and what to do...
Last edited on
why are you using the value of 16 directly?

If you want to find out the size of a type or object - then use sizeof(type) or sizeof(object)

so in this case:
long int num_of_terms = sizeof(n) / sizeof(long double)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

long double average(long double n[],long int num_of_terms)
{
    long double sum=0;
    long int i;
    for(i=0; i<num_of_terms; i++)sum+=n[i];
    return sum/num_of_terms;
}

int main()
{
   long double n[]={1,2,3,4,5,6,7};
   cout<<average(n,sizeof(n)/sizeof(long double) )<<endl;
}
Last edited on
thats great, but it doesnt explain why the value of num_of_terms is always 0 when sizeof(n)/16 is a variable rather than a parameter
Last edited on
** your second method which I have shown below will not work**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
long double average(long double n[])
{
    long int num_of_terms=sizeof(n)/16;//problem here - see comment below
    long double sum=0;
    long int i;
    for(i=0; i<num_of_terms; i++)sum+=n[i];
    return sum/num_of_terms;
}


int main()
{
    long double n[]={1,2,3,4,5,6,7};
    cout<<average(n)<<endl;
}


It will not work because the parameter being passed to the function is
only a pointer to the array not the array itself - so sizeof(n) will only report
4 bytes or so (the size of a pointer).
So long int num_of_terms=sizeof(n)/16; will be 4 /16 = 0

so return sum/num_of_terms; will a whatever sum happens to be DIVIDED BY 0. In this case it is 0.0/0
Divide by zero is a fatal error - it either crashes the program or is this case
the system catches it and reports NAN
Last edited on
that makes sense... so how would I be able to find the size of the array and not the pointer in the code you copied above???
Last edited on
It's impossible to know the size of an array, unless it's been created in the stack, and then only from the function that created it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void f2(int *);

void f(){
	int v1[4];
	//Ok. The size of v1 is known because it was created in the stack and we're
	//inside the function that created it.
	int size1=sizeof(v1)/sizeof(*v1);
	int *v2=new int[4];
	//Wrong. v2 was created in the heap, not in the stack.
	int size2=sizeof(v2)/sizeof(*v2);
	f2(v1);
}

void f2(int *array){
	//Wrong. This function did not create v1, so it doesn't know how big it is.
	//Furthermore, at this point it's impossible to know the size of array.
	int size1=sizeof(array)/sizeof(*array);
}

The only way for other functions to know the size of an array is by passing a size parameter (e.g. void f(int *array,int size);).
Last edited on
Topic archived. No new replies allowed.