c - Passing array into function not giving correct answer -
this question has answer here:
i wrote function :
string par(int a[]){ int s=sizeof(a)/sizeof(*a); cout<<s<<endl; /* ..do */
}
the main function written as:
nt main(){ int a[]={1,5,11,5}; cout<<sizeof(a)/sizeof(*a)<<endl; cout<<par(a)<<endl;
}
the output is:
4
1
while believe should same passed same array. kindly point out mistake..thanks..
in c++ when pass array argument function, you're passing pointer array.
since size of pointer , int 4 or 8 (depending on abi, , since you're getting 1 guess have 32-bit machine) you're getting 4/4 1.
so
int s=sizeof(a)/sizeof(*a);
1.
you should pass size argument instead of trying calculate inside function:
string par(int a[], int size)
Comments
Post a Comment