(e-mail address removed) wrote:
i want to get the address of buf, which defined as
char buf[] = "abcde";
[so that I can call the nonstandard strsep() function,
which we see elsethread requires the address of an
object of type "char *"].
This is of course correct. See
http://web.torek.net/torek/c/pa.html
for more on the subject. Unfortunately, it is also useless in this
case. (Indeed, it is rarely useful at all.)
no, (&buf == buf )&&(buf == &buf[0]) is TRUE.
No: &buf has type "char (*)[6]", because "buf" is an object of
type "array 6 of char". In the above expression, the second
occurrence of "buf" undergoes the transform dictated by The Rule,
and becomes a value of type "char *". A value of type
"char (*)[6]" is not compatible with a value of type "char *",
and a diagnostic is required (after which a compiler can do
as it pleases, including cease compiling forevermore
).
it seems if buf defined as above, there is no way to acquire the
address of buf itself.
Again, this is wrong: you *can* get "the address of buf itself",
but what you get is the address of the entire array. This is not
what you need, because the strsep() function demands the address
of an object of type "char *".
it needs the 3rd variable for the purpose?
This is no doubt the best way.
The strsep() function demands the address of an object of
type "char *".
In order to satisfy the strsep() function -- which, let me repeat,
demands the address of an object of type "char *" -- you must
create an object of type "char *". Once you have created an
object of type "char *", you have an object of type "char *"
whose address you can take, so as to give it to strsep(), which
demands the address of an object of type "char *".
Whenever any function demands the address of an object of type T
-- for any type T -- you must create an object of type T. There
are only two ways to create objects: you can either declare a
variable[%], or you can call malloc(). There are lots of ways to
create values in C, but only two ways to create objects, so use
one of those two. You only need to use malloc() in some special
cases, and until those cases occur, your best bet will be to declare
variables.
-----
[%] A formal parameter to a function also creates an object: in
void f(char *p) { ... }
p acts like a local variable. Some might argue that it is
not a "regular variable", but in most ways, it does qualify
as a regular variable, and I consider these to be ordinary
local variable declarations.
-----
There are many C functions that require the address of an object.
Fortunately, C makes it easy to compute the address of an object,
given a variable of the appropriate type: just write "&var", where
var is a variable of type T, to obtain a value of type "pointer to
T", pointing to the variable in question.
There are also many C funtions that require the address of the
first element of an array of objects, and C has a peculiar rule by
which the name of an array of N objects of type T turns into a
value of type "pointer to T", pointing to the first element of that
array. This rule is arbitrary, and is a special case, no matter
how common it winds up being; one must not allow the fact that
virtually every C program uses this funny rule dozens, hundreds,
thousands, or even millions of times, to distract from its
special-case-ness and mislead one into thinking that it is more
general than it is. Thus, while you need not do anything special
to call (e.g.) the fgets() function to fill in an array, you *do*
need to do something special to obtain the "char *" object that
strsep() demands, to pass the address of that object to strsep():
char buf[] = "some initial contents";
char *bp, *result;
...
buf = &buf[0]; /* or: char *bp = buf; */
result = strsep(&bp, delim);
The "temporary variable" bp really *is* required. (Well, you
could use malloc() instead, but that would be kind of silly.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.