(Walter Roberson) writes:
> In article < .com>,
> <> wrote:
[snip]
>>string s = "BB";
>
> C has no 'string'.
>
>>char buf[5];
>>sprintf(buf, "%04s", s.c_str());
>
> In C, that would imply that s is a structure (or union) with a member
> named c_str which is a function with no parameters. But you can't
> store functions in C, only function pointers, so you certainly
> wouldn't be getting anything useful there.
No, it implies that s is a structure or union with a member named
c_str which is a *pointer* to a function with no parameters.
Remember, the C function call operator takes a pointer-to-function as
it first operand. In the common case of using a function name, such
as foo(10, 20), the function name foo is implicitly converted to a
pointer-to-function value before it becomes the operand of the call
operator.
<OT>
The difference between this and a call to a C++ member function is
that there's no implicit parameter referring to the object containing
the pointer. s.c_str() is perfectly legal in C, assuming s is
declared properly, but the function pointed to by c_str has no
knowledge of the value of s -- which is why you don't see this kind of
thing as often in C as in C++. Something like s.c_str(s) would be
closer to the C++ semantics.
</OT>
This is, of course, a tangent of no particular relevance to what the
OP was actually asking about.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.