Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of elements of a C-array like this? [duplicate]

Tags:

arrays

c

I have a C-array like this:

double arr[] = {
    0.0,    0.1, 0.2, 0.5,
    0.1,    0.2, 0.3, 0.4,
    0.2,    0.0, 0.1, 0.3
};

Is it safe to get the number of elements like this?

int numElements = sizeof(arr)/sizeof(arr[0]);

Or is there a better way?

like image 772
openfrog Avatar asked Oct 23 '25 15:10

openfrog


1 Answers

This is the best way. Even it is suggested in C programming book by Dennis ritchie.

like image 134
user2550754 Avatar answered Oct 26 '25 06:10

user2550754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!