How to get number of elements in the array of pointers?

How to get number of elements in the array of pointers?



How to get number of elements in array of pointers?



Below is my code:


struct mystruct

int a;
char ch[10];
int c;
;

mystruct *m_arr[2];



I am traversing this array in some other files. Instead of hard-coding as 2 in every file I want to get the number of elements in the array programmatically.





You're programming in C++. So use std::vector.
– rustyx
Aug 27 at 8:49


std::vector





Or std::array<mystruct,2> if the size is really fixed. Either way, pick a language. You're using a mix of C and C++ here.
– MSalters
Aug 27 at 8:51


std::array<mystruct,2>





std::begin and std::end should work for arrays as well
– Andrew Kashpur
Aug 27 at 9:04





@AndrewKashpur: Wouldn't work with extern mystruct *m_arr[ ]. This question just contains the definition of m_arr, but there is only one Translation Unit with the definition. The rest o the code deals with the declaration.
– MSalters
Aug 27 at 12:51



extern mystruct *m_arr[ ]


m_arr




7 Answers
7



Don't use raw arrays. Use a standard container like std::vector or std::array. Both of these have a .size() member, and allow the range-based for syntax:


std::vector


std::array


.size()


for (mystruct* p : m_arr)



If you need C compatability, they both offer a data() member function which returns a pointer to the first element in the underlying array. (In your case, that will be a mystruct **)


data()


mystruct **



Edit: A raw array also supports the range-based for syntax - but only if the visible declaration includes the number of elements (so my_struct* m_arr[2]; is fine, but my_struct* m_arr would not work). It is impossible to declare a std::array without defining the size too.
Other containers (like std::vector)
don't include the size in the declaration.


my_struct* m_arr[2];


my_struct* m_arr


std::array


std::vector



The usual way of doing this is:


size_t sizeOfArray = sizeof(m_arr)/sizeof(m_arr[0]);



About size_t from wiki:


size_t



size_t is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The sizeof operator yields a value of the type size_t. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the header (cstdint header in C++). size_t is guaranteed to be at least 16 bits wide.





On some platforms you can use the _countof macro: size_t sizeOfArray = _countof(m_arr);
– Jabberwocky
Aug 27 at 9:09


_countof


size_t sizeOfArray = _countof(m_arr)



If this is a header file included by all other source code files using m_arr you can use sizeof(m_arr)/sizeof(m_arr[0]) to obtain the number of elements in the array. But this is really dangerous. If at some point the pointer m_arr enters a function as a parameter the scope of the function will not return 2 at sizeof(m_arr) but the number of bytes which the pointer takes in the memory which probably is 8.


m_arr


sizeof(m_arr)/sizeof(m_arr[0])


m_arr


sizeof(m_arr)



So if you want to stick to plain C then you have to pass the number of elements in a separate variable. But if you can use C++ there is a variety of better, safer and even faster solutions.



You can always do such memory arithmetics:


size_t arraySize = sizeof(m_arr) / sizeof(m_arr[0]);



But if you don't have a reason like in When would you use an array rather than a vector/string?:



Then you should use a std::array<mystruct*, 2> m_arr;, the size of which you can access through m_arr.size(), and the address of the underlying native array you get with m_arr.data().


std::array<mystruct*, 2> m_arr;


m_arr.size()


m_arr.data()



In C++17 you can #include <iterator> and use std::size(m_arr).


#include <iterator>


std::size(m_arr)





Or implement it in C++14 yourself (not an overly complex construct). I don't believe it depends on any core language changes in C++17.
– StoryTeller
Aug 27 at 9:17



Yes, use std::vector, but if you must...



this works for c++11 or greater:


template <typename T, std::size_t N>
constexpr std::size_t sizeofArray(T(&)[N])
return N;



Saw this recently in a video about 7 Features of C++ You Can Adopt Today



Assuming that you have your array as the following:


char *array[3];



Add an extra item to the array, which is considered to serve as an array terminator.


char *array[3]="First element","Second element","";



The rest of it depends on your own coding style, you can check for the terminator and count the number of elements except the terminating string.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế