Am Dienstag, dem 21.11.2023 um 02:30 +0000 schrieb André Albergaria Coelho via 
Gcc:
> Hello
> 
> #include <stdio.h>
> 
> void func(char *ptr) {
>      printf("\n%i",sizeof(ptr));
> }
> 
> int main(void) {
>      char arr[10];
>      printf("\n Sizeof arr %i",sizeof(arr));
>      func(arr);
> 
>      return 0;
> }
> 
> /* sizeof(arr) != sizeof(ptr), but they point to same thing. */
> 
> 
> So problem. On main, arr has size 10, while on func, arr has size 8. But 
> they are equal.

No problem. sizeof(ptr) is the size of the pointer object
itself, while arr is the size of the array. 

In  func(arr)  the array is converted to a pointer to
its first element.

If you want to pass the address of the array itself and
then get its size in 'func' you could write it like this:

#include <stdio.h>

void func(char (*ptr)[10]) {
     printf("\n%i",sizeof(*ptr));  // sizeof pointer target
}

int main(void) {
     char arr[10];
     printf("\n Sizeof arr %i",sizeof(arr));
     func(&arr);        // pass address to array

     return 0;
}


Martin


> 
> 

Reply via email to