I have a structure of which I will need an array. I have to pass that array as an argument and iterate that array. So, How do I determine the size of the array in the function? I know we can pass the size as another argument, but I am trying to avoid passing second argument. Thank you in advance.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int weight;
int profit;
}Object;
void IterateArray(Object *objects) {
int size = sizeof(objects) / sizeof(objects[0]);
printf("Size: %d", size);
//for (int i = 0; i < size; i++) {
//}
}
void main() {
int n = 5;
//Object objects[n]; // Can't run this
Object *objects = (Object*)calloc(n, sizeof(Object));
printf("Size of objects: %d\n", sizeof(objects));
printf("Size of objects[0]: %d\n", sizeof(objects[0]));
IterateArray(objects);
}
For the above code I am getting following output.
Size of objects: 4
Size of objects[0]: 8
Size: 0