Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
Recently, I learn C language, I have a problem. The example as follow:
C#
// function prototype
int numOfArray(int num[]);
int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    printf("the number of array is: %d.\n", sizeof(a)/sizeof(a[0]));
    printf("\n");
    printf("after calling function.\n");
    printf("the number of array is: %d.\n", numOfArray(a));

    return 0;
}

int numOfArray(int num[])
{
    return sizeof(num)/sizeof(num[0]);
}

The result is: the number of array is:9.
after calling function.
the number of array is:1.
As we know, array is transmitted in function through address. Why the two results are different? I hope someone can help me. Thanks.
Posted

1 solution

The value of a sizeof expression is determined during compiling period, and the one in int numOfArray(int num[]) can not figure out the original size. num is pointer there.
 
Share this answer
 
Comments
xushih 23-Nov-10 1:44am    
I see, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900