Firstly before I explain, here is the code in C:
#include <stdio.h>
void printSize(char* str);
int main()
{
char a[100] = "StackOverflow";
printSize(a);
return 0;
}
void printSize(char* str)
{
int n = sizeof(str) / sizeof(char);
printf("%d" ,n);
}
As you can already see, I am trying to get my function to print the value "100" as 100 is the size that I have allocated to my char
array a
. However I am getting 8 as my answer. Now I know why I am getting 8 here because it's printing the size of the pointer. So my question is, if I can't use this method to print out the value of "100" then how can I find out the memory allocated to a
from inside the function?
Thanks