There is a function ( getmymass ) which returns a pointer to an array. In the main method, I do this:

 int *mass; mass = GETMYMASS(); 

All elements are transmitted if mass[x] . But to get the dimension of the array in the following way: sizeof(mass)/sizeof(int) - does not work. Returns one ( 4/4 ). What is the problem?

    2 answers 2

    sizeof (mass) / sizeof (int) returns the pointer size and the size for the int type, they are the same. It is impossible to know the size of the array in memory, it must be transferred separately.

    • Or in memory to write any delimiter. Then you can read the pointer to the separator. - Deadkenny
    • 3
      and it's even easier to use some vector - DreamChild

    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); // Не забываем 
    • yes, and if the dimension of the array is unknown to me? I calculated it using a cycle, i.e. it turns out two identical cycles - one determines the length of the array, the second sets the values. How to avoid it? Can I do this: int * mass; first use malloc to initialize a zero value and add realloc memory in a loop? - Andrey Anfilets
    • one
      See how to use cplusplus.com/reference/vector/vector or if you have a builder, then TList. And edit your question with the array creation code, then it will be clear what you mean. - Deadkenny
    • @Andrey Anfilets The dimension of the array should be known for sure. In C, there are no multidimensional arrays in principle. But we can do two things: declare pointers of arbitrary depth (pointer, pointer to pointer, etc.) and allocate a continuous memory area of ​​the required length (buffer). With these tools, we can emulate multi-dimensional arrays. For the two-dimensional special case, we can do, for example, a pointer to an array of pointers, each of which points to the beginning of its buffer (string). - free_ze
    • @Andrey Anfilets const int ROWS_N = 3; const int COLS_N = 3; // Array of pointers to strings int ** arr = new int * [ROWS_N]; // Allocate memory for each row for (int i = 0; i <ROW_N; ++ i) arr [i] = new int [COLS_N]; // Use arr [i] [j] = ... // the same as * (* (arr + i) + j) = ... For R ^ 3, this will already be a pointer to a pointer to a pointer that points to a double pointer to an array of single pointers. That is, we have linked one-dimensional arrays into two-dimensional ones, and now two-dimensional arrays are concatenated into three-dimensional ones. - free_ze
    • one
      @Andrey Anfilets all of you are strongly advised to use the template class of the dynamic array std :: vector. In fact, there will be the same nesting principle: vector <int> for a one-dimensional array, vector <vector <int >> for a two-dimensional array, etc. But there already is the size () method, with which you can find out the number of elements of the vector (but not the dimension). - free_ze