LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

function to get the number Of Elements of a matrix, i.e., size or length

Is there such a function to get the number Of Elements of a matrix, i.e., size or length? Thanks a lot!
0 Kudos
Message 1 of 3
(3,181 Views)
Hi cviuser518,

The C function sizeof(variable) can be used to get the number of bytes taken up by the matrix or array.  Then divide by the number of bytes taken by the data type.  So for an array of ints, sizeof(array) / 4 will equal the number of elements.

Gavin Fox
Systems Software
National Instruments
0 Kudos
Message 2 of 3
(3,151 Views)
Note, however, that sizeof is only useful if your array is declared statically.  That is, if you use malloc to allocate an array dynamically, then use sizeof on the pointer that is returned, you'll always get 4, which is the number of bytes in a pointer.

In C, the bookkeeping of array/matrix sizes is left largely up to the programmer.  You'll notice that many functions that take array arguments (for reading or writing) also take an extra length argument because there is no way to tell the length from just the array alone.  So if you have to write functions that pass around arrays/matrices, have them take in the length/dimensions as well.

Hope this helps.

Mert A.
National Instruments
0 Kudos
Message 3 of 3
(3,147 Views)