Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print the last element of an array in c

Tags:

arrays

c

I want to detect the last element of an array of integer in C code. Otherwise i need to stop handling array data when the array element is empty. really my array have a maximum size fixed and it was filled from an external file so i dont know when it stop filling the array.

like image 473
gerrard2461 Avatar asked Oct 23 '25 19:10

gerrard2461


2 Answers

Last element is in position

sizeof(array)/sizeof(array[0]) - 1
like image 145
Ollaw Avatar answered Oct 26 '25 08:10

Ollaw


There is no defined term as empty array

Your array will always hold some value even if you dont initialize it explicitly

You need to define in your application how will you term it as empty may be by considering if its(element of array) value is 0 or some other value

like image 39
Pras Avatar answered Oct 26 '25 09:10

Pras