A
Anders Koeln
Could someone comment on this code please.
1. Are the comments in it correct?
2. Why does sizeof(arr) not work consistently in C? In someFunction()
sizeof(arr) means sizeof(&arr[0]) in main. The compiler knows how big arr
is, so why the difference - esp. as it would be v.useful if sizeof(arr)
worked in someFunction as it does in main - so you could use sizeof(arr)
in
the conditional bit of,say, a for loop in someFunction for example?
3. Why doesn't C allow testFunction to be declared char [] testFunction -
seems to me that if it did you could get away with knowing less about
pointers, and it seems as though again that C is incosistent, i.e., you
don't have to declare testFunction's parameter as char *. Surely, the
compiler should know that char [] someFunction actually means char *
someFunction??
char * testFunction(char []);
int main()
{
/* Lie to the compiler. Should be ok though as the compiler knows how
big the array really is.*/
char arr[7] = "0123456789";
/* Ouput the sizeof the array. And the size of a pointer to it. */
printf("main() sizeof arr = %d\n", sizeof(arr));
printf("main() sizeof arr = %d\n", sizeof(&arr[0]));
/* Pass it on to the function where we'll do some other emaningless
things.*/
testFunction(arr);
return(0);
}
/* Can't declare testFunction as char [] testF...() - even though the
compiler knows that array's are passed by address.*/
/* The size is ignored - and can't be checked anyway.*/
char * testFunction(char arr[500])
{
int n;
/* Same sizeof line of code that was in main() - note here that it
output the size of a pointer!*/
printf("sizeof arr = %d\n", sizeof(arr));
return(arr);
}
1. Are the comments in it correct?
2. Why does sizeof(arr) not work consistently in C? In someFunction()
sizeof(arr) means sizeof(&arr[0]) in main. The compiler knows how big arr
is, so why the difference - esp. as it would be v.useful if sizeof(arr)
worked in someFunction as it does in main - so you could use sizeof(arr)
in
the conditional bit of,say, a for loop in someFunction for example?
3. Why doesn't C allow testFunction to be declared char [] testFunction -
seems to me that if it did you could get away with knowing less about
pointers, and it seems as though again that C is incosistent, i.e., you
don't have to declare testFunction's parameter as char *. Surely, the
compiler should know that char [] someFunction actually means char *
someFunction??
char * testFunction(char []);
int main()
{
/* Lie to the compiler. Should be ok though as the compiler knows how
big the array really is.*/
char arr[7] = "0123456789";
/* Ouput the sizeof the array. And the size of a pointer to it. */
printf("main() sizeof arr = %d\n", sizeof(arr));
printf("main() sizeof arr = %d\n", sizeof(&arr[0]));
/* Pass it on to the function where we'll do some other emaningless
things.*/
testFunction(arr);
return(0);
}
/* Can't declare testFunction as char [] testF...() - even though the
compiler knows that array's are passed by address.*/
/* The size is ignored - and can't be checked anyway.*/
char * testFunction(char arr[500])
{
int n;
/* Same sizeof line of code that was in main() - note here that it
output the size of a pointer!*/
printf("sizeof arr = %d\n", sizeof(arr));
return(arr);
}