Arrays in C / C ++ do not store size. A pointer to an array is only a pointer to its first element, and the indexing operation arr[i]
refers to the memory area with offset i
from the pointer, i.e. equivalent to writing *(arr + i)
. Size must be stored separately.
(Lyrical digression) For string arrays in C, the agreement is to add a special marker element to the end — a null character or grain terminator, denoted by '\ 0', and the lines themselves are called "null / zero-terminated string". In this way, any string function, for example strlen (const char *), goes from pointer to start across the entire line until it encounters '\ 0', although the buffer itself may be longer than the position '\ 0', which is considered a conditional end lines.
In your case, you can pass a pointer to the buffer as an output argument and return its size.
/* C'шная функция */ size_t getMyArray(int **outPtr) { size_t arraySize = 333; *outPtr = (int*) malloc( sizeof(int) * arraySize ); return arraySize; } // Использование int *iPtr = nullptr; size_t size = getMyArray(&iPtr); for(size_t i=0; i<size; ++i) { cout << iPtr[i] << endl; } free(iPtr); // Не забываем