Is there a way to print the length of an array that contains element 0, and this array was passed to a function that can only have one argument as an array?
Example:
int arrLen(int arr[]) { //get size here; } int main() { int arr[]={0,1,2,3,4,5}; arrLen(arr); }
In C ++, there is a restriction that we cannot compare array elements with NULL if it has zero, but still asks if there is a way to do this. I can only pass an array of functions - this is my limitation.
In your example, you can use a function template to get what you want:
template <size_t N> int arrLen(int (&arr)[N]) { return N; }
, . , , , . , sizeof - , sizeof , .
sizeof
, , '\0', , ( , , , , -1 - ). .
'\0'
-1
C, , . , .
:
int arr[] = {-1,0,1,2,3,4,5}; arr[0] = sizeof(arr) / sizeof(arr[0]) - 1;
arr[0].
arr[0]
, :
\0
, , (, INT_MIN ), , . .
INT_MIN
, , , int ( ), -1, . arrLen, ( , , int):
arrLen
int arrLen(int arr[]) { int size = 0; int* p = arr; while (*p != -1) { ++size; ++p; } return size; }
, , "" , :
int arr[]={0,1,2,3,4,5, -1 };
, 1 , . , , . , , , , int .
- int ascii int, NULL '\ 0' '0', NULL .
, , .
Source: https://habr.com/ru/post/1526734/More articles:Why doesn't the N3421 provide a noexcept classifier? - c ++Queue Limitation Over Time - rabbitmqLaravel: Redirect to custom class? - phpBest practice for creating a button in an Android app? - android"Error 87, this option is not recognized in this context", using DISM to enable IIS - iisMachine learning model save options - pythonjavax.faces.view.facelets.FaceletException: error analysis /template.xhtml: object "nbsp" referenced but not declared - doctypeI need to check if an enumeration element is included in an enumeration set - javacreate table without specifying in db / migrate rails - ruby | fooobar.comкак получить миниатюру сообщения с помощью сообщения id в wordpress? - phpAll Articles