We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hi i wrote this code which is written bellow when ever i check the value of u it appear to be the 3. while it is supposed to be 8. Can any body tell me what is the problem with this code and how can i get the exact size of the arrgument of func.
void main(){ func("fara.txt"); } void func(unsigned char arr[]){ int u; u=sizeof(arr); }
The argument you are passing is effectively a pointer. 3 bytes is the correct size for a C51 Generic Pointer (see Manual for details).
The problem is not with sizeof itself, but that you are applying it to an unbounded array (arr[]). The way you have the 'arr' parameter declared, it degrades to a pointer which, being unqualified, is a generic pointer whose size is 3. Compare this example:
void main() { func("fara.txt"); } void func(unsigned char arr[]) { int u; u = strlen(arr); }
void main() { func("fara.txt", sizeof "fara.txt"); } void func(unsigned char arr[], size_t len) { int u; u = len; }
char str[] = "fara.txt"; void main() { func(str, sizeof str); } void func(unsigned char arr[], size_t len) { int u; u = len; }
I should note that the value assigned to 'u' in example #1's func() is different than the value assigned in examples #2 and #3. Why is left as a simple exercise for the reader.
That is not an appropriate use of sizeof. Try using strlen. Jon
"Try using strlen." Assuming that we're talking about null-terminated strings...
int u,v; u = sizeof( "Hello World\n" ); v = strlen( "Hello World\n" );
hi, In fact, sizeof() is not a real function; it is most like compiler directive. It takes that in brackets, and calculates its size. Here you pass the pointer, which size has been calculated. When you need to know the size of the string then the function strlen() should be used. Note that you should take care about that string must be null-terminated:
void func(unsigned char arr[]){ int u; u=strlen(arr); }
void func(unsigned char *arr){
unsigned char arr[]= {"fara.txt"}; // ... u=sizeof(arr);
"It is left as another excercise for the student to explain why u and v receive different values in the above example..." In case the student is left even more confused by this: u==13 //Includes null terminator v==12 //Doesn't include null terminator
Even in theory, sizeof is not a function. It is an operator. It seems to be a nearly universal convention to write the expression as "sizeof (X)", so it looks like a function. But "sizeof X" would also be correct. X is evaluated at compile time to determine the type of the expression (only); X is not actually evaluated nor is any code generated. While I'm at it, return is not a function, either. It's a keyword. So, the fairly common convention of including the return value in parens: return (result); is unnecessary. exit() is a function; return is not.
"But "sizeof X" would also be correct." If X is a type name, parentheses are required. In fact, it is only when the sizeof operator is used to obtain the size of a type, that the parentheses are required.