how can i get the address of buf which defined as char buf[] = "abcde";

B

baumann.Pan

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


thanks

baumann@pan
 
A

Artie Gold

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


thanks

baumann@pan

1) There is no such function as strsep() in standard C.
2) To use it, you'd have to do something like:

char buf[] = "abcde";
char *p = buf;
char *q = strsep(&p, "c");

HTH,
--ag
 
P

pete

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

strsep(&buf, pointer to token);

The pointer to the string is buf,

strsep(buf, pointer to token);

.... depending on what you really want.
 
A

Artie Gold

pete said:
hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


The address of buf is &buf.

strsep(&buf, pointer to token);

That's true -- but useless, as `buf' is immutable.
The pointer to the string is buf,

strsep(buf, pointer to token);

... depending on what you really want.

[The signature is:

char *strsep(char **stringp, const char *delim);

--hence the problem.]

--ag
 
P

pete

Artie said:
hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


The address of buf is &buf.

strsep(&buf, pointer to token);

That's true -- but useless, as `buf' is immutable.

What difference does it make that buf is immutable?
 
B

baumann@pan

pete said:
hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

buf is the pointer to the location of the string "abcde".

it seems if buf defined as above, there is no way to acquire the
address of buf itself.

it needs the 3rd variable for the purpose?
 
B

baumann@pan

Artie said:
pete said:
hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


The address of buf is &buf.

strsep(&buf, pointer to token);

That's true -- but useless, as `buf' is immutable.

buf is mutable, if buf is defined as char * buf ="abcde"; then you are
right;

The pointer to the string is buf,

strsep(buf, pointer to token);

... depending on what you really want.

[The signature is:

char *strsep(char **stringp, const char *delim);

--hence the problem.]

yes it is defined in linux conforms to bsd4. not conforms to ansi C.
 
P

pete

pete said:
Artie said:
(e-mail address removed) wrote:

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


The address of buf is &buf.

strsep(&buf, pointer to token);

That's true -- but useless, as `buf' is immutable.

What difference does it make that buf is immutable?

That's OK.
I googled strsep, so now I know what it is.
 
P

pete

baumann@pan said:
hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

Without a cast, (&buf == buf) is undefined.

&buf is most definitely the address of buf.

What do you think &buf is?
 
B

baumann@pan

pete said:
baumann@pan said:
(e-mail address removed) wrote:

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

Without a cast, (&buf == buf) is undefined.

&buf is most definitely the address of buf.

no, buf ,&buf and &buf[0] is the address of "abcde", not address of
variable buf. i think we can not get the address of buf itself.
 
B

baumann@pan

pete said:
baumann@pan said:
(e-mail address removed) wrote:

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

Without a cast, (&buf == buf) is undefined.

&buf is most definitely the address of buf.

assume "abcde" starts at the address of 0 and ends at address 5;

then buf = 0; &buf = 0 too.

we don't know what's address of buf itself.
 
P

pete

baumann@pan said:
baumann@pan said:
pete wrote:
(e-mail address removed) wrote:

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

Without a cast, (&buf == buf) is undefined.

&buf is most definitely the address of buf.

no

You don't know what you're talking about.
 
C

Chris Torek

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.)
 
P

pete

baumann@pan said:
baumann@pan said:
pete wrote:
(e-mail address removed) wrote:

hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);

The address of buf is &buf.

no, (&buf == buf )&&(buf == &buf[0]) is TRUE.

Without a cast, (&buf == buf) is undefined.

&buf is most definitely the address of buf.

assume "abcde" starts at the address of 0 and ends at address 5;
then buf = 0; &buf = 0 too.

If that were true then (buf == "abcde") would be true also,
but it isn't.
we don't know what's address of buf itself.

One of us does.

Seriously, what do you think (&buf) means?

Get your compiler into conforming
mode and compile the following program.
What diagnostic message does your compiler have to give you?

int main(void)
{
char buf[] = "abcde";

return &buf - buf;
}
 
A

Artie Gold

baumann@pan said:
Artie said:
pete said:
(e-mail address removed) wrote:


hi all,

i want to get the address of buf, which defined as

char buf[] = "abcde";

so can call
strsep(address of buf, pointer to token);


The address of buf is &buf.

strsep(&buf, pointer to token);

That's true -- but useless, as `buf' is immutable.


buf is mutable, if buf is defined as char * buf ="abcde"; then you are
right;
No, the *contents* of buf are mutable, `buf' itself is not.
The pointer to the string is buf,

strsep(buf, pointer to token);

... depending on what you really want.

[The signature is:

char *strsep(char **stringp, const char *delim);

--hence the problem.]
--ag
 
B

baumann@pan

&buf[0] = buf = &buf is __TRUE__ only if we don't consider the data
type, but the value only.

we can cast the three to void * , a pointer.

(void*)&buf[0] has the same value of (void*) &buf and same value of
(void*)buf.

you all should notice

char * buf2 = (char*)mallic(100);

in this case we can get the address of buf2 by &buf2;

but not &buf;

buf is same as buf2 which pointer to the location where the object
lies.




Chris said:
(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.
 
B

baumann@pan

Chris said:
(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 :) ).

i mean they are equal by value not by their data type.
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

the address of entire array is the address of the object. and in this
it's also address of that variable.




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.
 
C

Chris Torek

[Given:
char buf[N];
for some suitable constant N]

&buf[0] = buf = &buf is __TRUE__ only if we don't consider the data
type, but the value only.

If you "don't consider the data type", then 3 == 3.14, and 3 != 3.14.
we can cast the three to void * , a pointer.

We can cast the 3.14 to "long", and then 3 == 3.14.

Or, we can convert (via cast or via ordinary cast-free conversion)
the 3 to "double", and then 3 != 3.14.

This shows that:

IF YOU IGNORE THE TYPE, YOU GET THE WRONG ANSWER.

Never, ever, ignore the type. C is *not* a typeless language!
(void*)&buf[0] has the same value of (void*) &buf

Probably, and perhaps even provably. (It would be hard, and perhaps
impossible, to get the rest of the C Standard's requirements right
while also doing something weird with array addressing that would,
say, encode the size of the array in the result of (void *)&buf,
so that it differs from (void *)&buf[0].) But this just means
that:

If you take value A, of type T1, and value B, of type T2,
and convert both to new values of type T3, the new values
compare equal.

This is also true for (int)3 and (double)3.14. Value A has type
int, value B has type double, and we convert both to new values of
type "char", "short", "int", or "long" -- we have lots of choices
for type T3 -- and they will compare equal.
and same value of (void*)buf.

Yes; this falls out from The Rule about pointers and arrays in C
(<http://web.torek.net/torek/c/expr.html#therule>). The "value"
of an array object is a pointer to the array's first element.
Hence:

&buf[0] == buf

is always true -- no casts are required.
you all should notice

char * buf2 = (char*)mallic(100);

in this case we can get the address of buf2 by &buf2;

Yes. "buf2" is an object of type "pointer to char", so &buf2 is
a value of type "pointer to (pointer to char)", pointing to the
object named buf2.
but not &buf;

Here, "buf" is an object of type "array N of T", so &buf is a
value of type "pointer to (array N of T)", pointing to the
(entire) object name buf.
buf is same as buf2 which pointer to the location where the object
lies.

No! Learning this is crucial to becoming a fully competent C
programmer: buf is *not* "the same as" buf2. The object named
"buf" is an array. The object named "buf2" is not an array.
The rules for computing their values therefore differ. The
results of &buf and &buf2 differ. The results of sizeof(buf)
and sizeof(buf2) differ.

The thing that is peculiar is that the *different* rules for
computing the "value" of buf and the value of buf2 can cause
these two different things to have the same value! That is,
after:

char buf[N]; /* for some suitable N */
char *buf2;

buf2 = buf;

the values equal, but the entities differ. The values are equal
because the "value" of an array object is that produced by The Rule
about arrays and pointers in C.

printf("buf = %p, buf2 = %p; but sizeof(buf) = %lu, sizeof(buf2) = %lu\n",
(void *)buf, (void *)buf2,
(unsigned long)sizeof buf, (unsigned long)sizeof buf2);

The different "sizeof" results proves that the two are not identical.
This is why The Rule says: "in a value context". Some operations
-- in particular, unary "&" and sizeof -- are not "value contexts".
 
M

Mark McIntyre

the address of entire array is the address of the object. and in this
it's also address of that variable.

Its worth pointing out to you that Chris Torek is one of THE top gurus
round here. You can almost guarantee he's right, and if he were wrong,
all the other gurus would point it out.

So.... if you're disagreeing with him its almost axiomatic that
you're mistaken.....
 
P

pete

Chris said:
(<http://web.torek.net/torek/c/expr.html#therule>). The "value"
of an array object is a pointer to the array's first element.

I don't think that what you mean by "value" "in quotes"
is very close to the meaning of value.
That an array should have the same "value" regardless
of it's contents or whether or not it's been initialized,
is too different from the meaning of value, for me.
The value of an object should depend on it's contents.

In this kind of an array definition:
char array[] = "ab";
the expressions on both sides of the equal sign are array types.
The initializing elements are derived from the contents of "ab",
or as I think of it, from the value of the array "ab".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top