NSString *lang = @"en";
const char* ar = [lang UTF8String];
int size_of_array = (sizeof ar) / (sizeof ar[0]);
size_of_array
is equal to 4
and (sizeof ar) = 4
and sizeof ar[0] = 1
.
Why? I think it (size_of_array
) has to be 2
.
NSString *lang = @"en";
const char* ar = [lang UTF8String];
int size_of_array = (sizeof ar) / (sizeof ar[0]);
size_of_array
is equal to 4
and (sizeof ar) = 4
and sizeof ar[0] = 1
.
Why? I think it (size_of_array
) has to be 2
.
sizeof ar
will get the size of the type char *
, which is a pointer and so takes 4 bytes in memory. You want to get the length of the string, so use the function strlen
instead of sizeof ar
It isn't clear what you are trying to do.
Your third line of code references an array "ar" that isn't declared anywhere in your post, and doesn't seem to relate to the code before it.
Also, the bit sizeof ar[]
doesn't make much sense. That will give you the size of a single element in your ar
array, whatever that is. So you are taking the size of the pointer variable ar, and dividing it by the size of one element in the ar
array.
Are you trying to determine the memory size of the ASCII string lang_ch?
If so, then you want
int size_of_array = strlen(lang_ch) + 1;
That will give you the length of the string you get back, including the null terminator.