0

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!

cafce25
  • 15,907
  • 4
  • 25
  • 31
myyou
  • 1
  • 1
  • Have you read your error message carefully? It clearly states your error: "`sizeof (char *) / sizeof (char)` does not compute the number of array elements". You'll have to pass those values in along with your pointer. – cafce25 Dec 01 '22 at 13:51
  • `sizeof((char *)arr[0])` does not make much sense. It gives you `sizeof(char *)`, independently of what `sizeof(arr[0])` would be. – mch Dec 01 '22 at 14:09

0 Answers0