This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem with sizeof

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);
}
thanks
Regards
Farhan Arshad

Parents
  • 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);
    }

    In example above, the function accepts the string pointer as parameter (in fact, you may replace it with
    void func(unsigned char *arr){
    
    As result, sizeof() does know about array nothing. It takes the pointer and calculates its size.

    However, it is possible to use sizeof() with an array. But array should be defined really:
    unsigned char arr[]= {"fara.txt"};
    // ...
    u=sizeof(arr);
    

    Regards,
    Oleg

Reply
  • 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);
    }

    In example above, the function accepts the string pointer as parameter (in fact, you may replace it with
    void func(unsigned char *arr){
    
    As result, sizeof() does know about array nothing. It takes the pointer and calculates its size.

    However, it is possible to use sizeof() with an array. But array should be defined really:
    unsigned char arr[]= {"fara.txt"};
    // ...
    u=sizeof(arr);
    

    Regards,
    Oleg

Children
  • 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.