can i do some calculation using the sizeof operator in c to get the rows count inside a function who receives as a parameter only a two dimensional array without any information about rows or columns count in this passed array?
void main(void){
int A[5][6];
int columnsC = sizeof(A[0]) / sizeof(int);
int rowsC = ( sizeof(A)/sizeof(int) ) / columnsC;
printf("rows: %d\ncolumns: %d\n", rowsC, columnsC);
getch();
}
the previous code worked for me, but i can't find a helpful resource to know how to apply this on a function of the following header:
void fun(int arr[][10])
i"m studying a course on Programming Languages Principles, and some times think of things differently and look for learning resources, and this helps a lot, but not this time apparently.
i admire any help.
No. There's no way find the size of an array when passed to a function. Array decays into a pointer when passed to a function.
This:
void fun(int arr[][10])
is actually syntatic sugar for:
void fun(int (*arr)[10])
Instead you can pass the size as a thrid argument or size as an element of the array itself.
I do not believe this is possible, unless you either pass a length, or you specifically allocate one extra row to the array. This extra row can contain a null character. In the function fun, then you can use a while loop to cycle through the array until you hit the null value.
int i = 0;
while (1) {
if(arr[i]==NULL){
//exits when hits end
break;
}
//do stuff with arr[i]
i++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.