This is the function I made.
#include <stdio.h>
int get_height(char **arr)
{
int i;
i = sizeof(arr) / sizeof((char *)arr[0]);
return(i);
}
int get_larger(char **arr)
{
int i;
i = sizeof(arr[0]) / sizeof(char);
return(i);
}
int main()
{
char arr[4][2] = { {'1', 'C'}, {'E', '5'}, {'2', 'c'}, {'3', 'D'} };
printf("size of arr : %lld\n", sizeof(arr));
int col = sizeof(arr[0]) / sizeof(char);
int row = sizeof(arr) / sizeof(arr[0]);
printf("size of col : %d\n", col);
printf("size of row : %d\n", row);
printf("get_height funtion: %d\n", get_height((char **)arr));
printf("get_larger funtion: %d\n", get_larger((char **)arr));
return 0;
}
I want to make a function to get the height.. It works inside the main function, but it's not working when create a separate function. I think I'm misplacing the pointers...
The result looks like this.
> gcc main3.c
> .\a.exe
size of arr : 8
size of col : 2
size of row : 4
get_height funtion: 1
get_larger funtion: 8
> gcc -Wall -Wextra -Werror main3.c
main3.c: In function 'get_height':
main3.c:7:22: error: division 'sizeof (char **) / sizeof (char *)' does not compute the number of array elements [-Werror=sizeof-pointer-div]
7 | i = sizeof(arr) / sizeof((char *)arr[0]);
| ^
main3.c:3:23: note: first 'sizeof' operand was declared here
3 | int get_height(char **arr)
| ~~~~~~~^~~
main3.c: In function 'get_larger':
main3.c:15:25: error: division 'sizeof (char *) / sizeof (char)' does not compute the number of array elements [-Werror=sizeof-pointer-div]
15 | i = sizeof(arr[0]) / sizeof(char);
| ^
cc1.exe: all warnings being treated as errors
help me world!