You are going about it in the wrong way. I'll try to explain using a small code example. The explanation is in the code comments:
int array[100];
int size = sizeof(array) / sizeof(array[0]); // size = 100, but we don't know how many has been assigned a value
// When an array is passed as a parameter it is always passed as a pointer.
// it doesn't matter if the parameter is declared as an array or as a pointer.
int getArraySize(int arr[100]) { // This is the same as int getArraySize(int *arr) {
return sizeof(arr) / sizeof(arr[0]); // This will always return 1
}
As you can see from the code above you shouldn't use sizeof
to find how many elements there are in an array. The correct way to do it is to have one (or two) variables to keep track of the size.
const int MAXSIZE 100;
int array[MAXSIZE];
int size = 0; // In the beginning the array is empty.
addValue(array, &size, 32); // Add the value 32 to the array
// size is now 1.
void addValue(int *arr, int *size, int value) {
if (size < MAXSIZE) {
arr[*size] = value;
++*size;
} else {
// Error! arr is already full!
}
}