Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I know this to find the size of array = sizeof(arr)/sizeof(arr[0])
But I have to implement the following (It’s just a demo):
demo.h
#ifndef __DEMO_H
#define __DEMO_H
void heap_sort(int *);
#endif
demo.c
void heap_sort(int *ptrA)
{
//implementing heap sort
But here it requires length of array
}
main.c
#include "demo.h"
int main(void)
{
int A[10];
heap_sort(A)
return 0;
}
FYI .. It’s just a demo.. but here I have to implement it in some other scenarios in which there is restriction that “DON’T CHANGE ANYTHING IN HEADER FILE” which means i can’t change the function signature . Then how to get the array length in demo.c For char it’s easy to get by help of strlen() Isn’t there anything similar to get the length of int,float double types
if you can’t change the function signature, then maybe you could pass the size of the array in the first element.
Or mark the end of the array with some special value, but I don’t like this one because you’d have to iterate the whole array to find the length and you need to make sure this value is not used in the array:
This is just a solution for the restrictions you imposed, what I would normally do, is either pass the size of the array as a second argument, or use a
structfor the array:Note: other horrible solutions include global variables.