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
  • 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);
    }
    with this example:
    void main() {
        func("fara.txt", sizeof "fara.txt");
    }
    
    void func(unsigned char arr[], size_t len) {
        int u;
        u = len;
    }
    and with this example:
    char str[] = "fara.txt";
    
    void main() {
        func(str, sizeof str);
    }
    
    void func(unsigned char arr[], size_t len) {
        int u;
        u = len;
    }

Reply
  • 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);
    }
    with this example:
    void main() {
        func("fara.txt", sizeof "fara.txt");
    }
    
    void func(unsigned char arr[], size_t len) {
        int u;
        u = len;
    }
    and with this example:
    char str[] = "fara.txt";
    
    void main() {
        func(str, sizeof str);
    }
    
    void func(unsigned char arr[], size_t len) {
        int u;
        u = len;
    }

Children