As i want to find array size dynamically in function, i used sizeof operator. But i got some unexpected result. here is one demo program to show you, what i want to do.
Inside getSize()
, you're getting size of pointer, which is 8 bytes (since you're probably running 64-bit OS). In main()
, you're getting size of array.
If you want to know array size, pass result of sizeof(S)
as additional argument to getSize()
.
More alternatives would be using some container (like std::vector
) or turning function into template function, as Nawaz proposed.