-1

I tried to get how many bytes are allocated for an array using standalone function but I failed. My code is as follows:

#include <stdio.h>

void testing(char *source);

int main(){

    char a[12] = "Hello World!";
    //printf("%d",sizeof(a));   Prints 12. I want to get this value
    testing(a); // Prints 4. I need to get 12 by using functions


return 0;
}

void testing(char *source){
    printf("%d",sizeof(source));
}

I want to get result as 12 but output tells me that it is 4.

Abdulla
  • 39
  • 1
  • 7

1 Answers1

0

I want to get result as 12 but output tells me that it is 4.

You can't get the size of an array when it's passed as pointer to function.

In void testing(char *source), source is pointer, so sizeof( pointer ) gives you 4 (4 bytes in your machine).

There is no way to find out the array size inside the function, as array parameter will decay to pointer anyway.

artm
  • 17,291
  • 6
  • 38
  • 54