#include <iostream>
@brief calculates and prints array size
@param string the array
void function(char* string){
int n = sizeof(string)/sizeof(char); //should be size of the array given
printf("%d",n); //prints INCORRECT size - 8
}
int main(){
char str[] = "abcd"; //size is 5
printf("%d",(sizeof(str)/sizeof(char)); //prints correct size - 5
}
The issue :
When I try to calculate the size of an array given with the formula in the code, it returns different values in main and in any function. The result in main block is correct, but in any function block, it calculates the wrong value. Why?
I tried calculating the size of an array, and was expecting to get it.