I am a C++ beginner and recently discovered making my own functions and libraries. So i thought that i would make a library as a project. I implemented a function in it, which returns the number of elements in an array. But as I found on most websites, and with my own understanding, using "sizeof(array) / sizeof(array[0]);" works... but not for me. I have an array of 5 elements and the compiler is giving the output "1". Here are the codes of both files:
Main.cxx:
#include <iostream>
#include "SML"
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
cout << Int.countof(arr) << endl;
return 0;
}
SML(Header):
// SMLlib:
// Simple Math Library for C++
// Created by Saadat Khurshid
// integer functions
class Int {
public:
// int array count
int countof(int arr[]) {
int count = sizeof(arr) / sizeof(arr[0]);
return count;
}
};
// Making object of Int class
Int Int;
Note: I am using clang-based Cxxdroid android compiler. All the features working on PC IDE's works on it(except ofcourse some windows only libraries).