Using sizeof on arrays passed as parameters [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using sizeof on arrays passed as parameters [duplicate]



Possible Duplicate:
Sizeof array passed as parameter



Given the function below I understand that sizeof returns the size of the pointer of the type in the array.


int myFunc(char my_array[5])

return sizeof(my_array);



However calling sizeof on an array not passed as a parameter normally returns the sizeof the array.



What causes this inconsistency? What is the best way of getting the size of an array when it is passed as a parameter?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





char[5] my_array is not a valid declarator.
– CB Bailey
Feb 25 '12 at 12:26


char[5] my_array





See: Sizeof array passed as parameter
– CB Bailey
Feb 25 '12 at 12:28





related FAQ
– fredoverflow
Feb 25 '12 at 12:31




4 Answers
4



What causes this inconsistency?



The name of the array decays as an pointer to its first element.

When you pass an array to an function, this decaying takes place and hence the expression passed to sizeof is a pointer thus returning pointer size.


sizeof



However, an array passed to sizeof always returns size of the array because there is no decaying to an pointer in this case.


sizeof



What is the best way of getting the size of an array when it is passed as a parameter?



Don't pass them by value, pass them by reference or

Pass size as an separate argument to the function.



You have to pass the array by reference; you cannot pass arrays by value (they decay into pointers to the first element):


int myFunc(char const (& my_array)[5])

return sizeof(my_array); // or just "return 5"



In case that you want to call this function with a reference to lots of different local arrays, you can use a function template instead to have the array size deduced:


template <unsigned int N>
int myFunc(char const (& my_array)[N])

return sizeof(N); // or "return N"



You should really make the functions return an unsigned int or an std::size_t, though. In C++11, you can also declare them as constexpr.


unsigned int


std::size_t


constexpr





This answer in addition to specifying how to get a given size of the array (to survive as a parameter), also gives an alternative route using templates to allow for multiple array sizes. Do note that the template version needs to live in the header file...
– holroy
Jan 14 '15 at 10:27



When you pass an array to a function you must call the function same as follow : myFun(my_array,size), where the size is an integer variable contains size of the array.(here it's myFun(my_array,5)) . and the signature of the function must be : int myFunc(char my_array, int size)



Avoid to use raw pointers, use std::vector instead of pointer to an array:


int myFunc( const std::vector<char>& buf)

return buf.size();

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard