1
1
123qwe2016-05-22 18:29:57
C++ / C#
123qwe, 2016-05-22 18:29:57

How to find the last element in an int array?

How to find the last element in an int array?
No vectors

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2016-05-22
@gbg

If the array is statically defined:

int alpha[100];

alpha[99]=4242;

const int last = alpha [ (sizeof(alpha) / sizeof(alpha[0]))-1];

If you only have a pointer to the beginning of the array, then nothing. Store the length separately.

F
Fat Lorrie, 2016-05-22
@Free_ze

Nothing, if you don't mark it.
To do this, in char-strings ("C-strings", "zero-terminated strings"), an element with a value '\0'(null character) is added to the end, on the presence of which standard functions rely, like strlen(const char*), which counts characters up to the first zero-terminator.
ZY Theoretically, there is still some allocation header, which is located before the dynamically allocated (that is, only in the heap) buffer, which is oriented free(void*)to, but this is not regulated by the standard in any way.
UPD: Since C++11 we have std::array, which actually wraps a buffer in an object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question