FAQ Clarification

This is a discussion on FAQ Clarification within the C Programming forums, part of the General Programming Boards category; Why gets() is bad / Buffer Overflows
In this FAQ, you state that "gets() has only received the name of ...

I always thought that "sizeof" was a resolved by the compiler at the compile time. So it only works on structures and arrays with known values. It does not work with arrays such as "char *p" as it will not know how many char's "p" points to.

Therefore "sizeof" can not be used. There are no other ways to determine the length of an array from the pointer alone.

[QUOTE]there is nothing you can do here which will
// reliably tell you that p points to 100 bytes[QUOTE]

If the array was declared to be a 100 bytes at compile time as in your example, and you CAN use sizeof(buff) / sizeof(*buff) to reliably tell you the length than why would the pointer *p in your function be any different than buff?

>So, it would'nt work if I malloc a character array dynamically, for example?

No, not there either.

[edit]
>If the array was declared to be a 100 bytes at compile time as in your example, and you CAN use sizeof(buff) / sizeof(*buff) to reliably tell you the length than why would the pointer *p in your function be any different than buff?

Why is the buffer pointer in your function any different than the line pointer in main? The line pointer just gets passed to your function, where it is stored in the buffer pointer, they should be equal so why does sizeof return different results?

>>But I thought any time you use the name of an array it is interpreted as a pointer to that array.

6.3.2.1 Lvalues, arrays, and function designators

Except when it is the operand of the sizeof operator or the unary & operator, or is a
string literal used to initialize an array, an expression that has type ‘‘array of type’’ is
converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
the array object and is not an lvalue.

Originally posted by Wledge >>But I thought any time you use the name of an array it is interpreted as a pointer to that array.

6.3.2.1 Lvalues, arrays, and function designators

Oh, I get it. Thanks for the reference to the ANSI C Standard. I guess I better go back to writing my getline function, which will properly get a line of input from the user and stop complaining about how crappy gets is. And while I'm at it maybe I'll reread the chapter on pointers in K & R's The C Programming Language.