Declares an integer array of length 10 within the main function and forwards it to the func function to output different lengths of the calculated array and the calculated array length within the main function.
#include <stdio.h>
void func(int * arr) {
int len = sizeof(arr) / sizeof(int);
printf("%d \n", len);
}
int main() {
int num[10];
printf("%d \n", sizeof(num) / sizeof(int)); // 10 Output
func(num); // 2 Output
return 0;
}
I thought num and arr would be calculated exactly the same, but why is it calculated differently?
c dev-c++
of handing over into the main function of function from func num
is just starting address of the array. That the func The function is only starting address of the array, because I can see only the information, have no way to know the size of the array.
It is common to use n
inside a function after changing the function prototype to the void func(int*arr, intn)
format and adding the array length to n
when calling the function.
For example, in the main function, run func(num, sizeof(num)/sizeof(num[0]))
.
917 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.