string buffer

G

George2

Hello everyone,


Suppose we defined a string buffer (array), like this,

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

example like,

strcpy (buf, array);
strcpy (&buf, array);
....

I am wondering the differences between the 3 approaches, and which
approach is the most correct?


thanks in advance,
George
 
P

pete

George2 said:
Hello everyone,

Suppose we defined a string buffer (array), like this,

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

example like,

strcpy (buf, array);
strcpy (&buf, array);
...

I am wondering the differences between the 3 approaches, and which
approach is the most correct?

The type of the parameters of strcpy is: pointer to char.

char *strcpy(char * restrict s1, const char * restrict s2);

When buff and array are used as arguments,
their types are converted to pointers to char.

The type of (&buf) is pointer to array of 256 char,
so that's the wrong type of argument for the parameter.

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] 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. If the array object has
register storage class, the behavior is undefined.
 
J

Johannes Bauer

George2 said:
char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

1 and 3 are semantically identical. I prefer 1.

2 is wrong for strcpy, as was already pointed out to you. It should at
least yield a warning in most compilers.

Greetings,
Johannes
 
V

vicks

George2 said:
char array[] = "hello world";
char buf[256]
Sometimes, I noticed that we either use,
1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

I have tried (buf,array) working i tried ((char *)buf,(const char
*)array ) that also is working..but when i tried (&buf,&array) it
given warning..

But i think the mpost app. syntax would be strcpy((char *)buf,(const
char *)array)?

Regards
Vikas
 
M

Mark Bluemel

vicks said:
I have tried (buf,array) working i tried ((char *)buf,(const char
*)array ) that also is working..but when i tried (&buf,&array) it
given warning..
But i think the mpost app. syntax would be strcpy((char *)buf,(const
char *)array)?

Why would you think that?

Given "char buf[<some number];", "buf" "degrades" to "char *", so the
first cast is totally unnecessary. (See 6.3.2.1 "Lvalues, arrays and
function designators").

If necessary, the "array" argument will be converted to "const char *"
as that's what the strcpy() prototype specifies it will be.

strcpy(buf, array) is simple, clear and readable.
 
J

Johannes Bauer

vicks said:
I have tried (buf,array) working i tried ((char *)buf,(const char
*)array ) that also is working..but when i tried (&buf,&array) it
given warning..

The compiler is giving a warning because it is clearly wrong, as pointed
out before.

Greetings,
Johannes
 
C

CBFalconer

George2 said:
Suppose we defined a string buffer (array), like this,

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

When passed as function parameters, all arrays are automatically
passed as pointers to the first item in the array. Thus by simply
using 'array' and 'buf' you are getting case 3.
 
K

Keith Thompson

CBFalconer said:
When passed as function parameters, all arrays are automatically
passed as pointers to the first item in the array.
[...]

True, but incomplete.

I've seen a lot of posters here say that arrays are converted to
pointers when passed as function arguments. That's correct, but it's
only a special case of a more general rule. An array expression is
converted to a pointer in *most* contexts, not just in function calls
(the only exceptions are when the array expression is the operand of a
unary "sizeof" or "&" operator, or when it's a string literal used to
initialize an array).
 
C

CBFalconer

Keith said:
CBFalconer said:
When passed as function parameters, all arrays are automatically
passed as pointers to the first item in the array.
[...]

True, but incomplete.

I've seen a lot of posters here say that arrays are converted to
pointers when passed as function arguments. That's correct, but
it's only a special case of a more general rule. An array
expression is converted to a pointer in *most* contexts, not just
in function calls (the only exceptions are when the array
expression is the operand of a unary "sizeof" or "&" operator, or
when it's a string literal used to initialize an array).

Why do you complicate the thread with this? It has nothing to do
with the OPs posting, to which I replied. That makes two similar
foolish postings in as many minutes.
 
K

Keith Thompson

CBFalconer said:
Why do you complicate the thread with this? It has nothing to do
with the OPs posting, to which I replied. That makes two similar
foolish postings in as many minutes.

It's called a discussion.
 
C

CBFalconer

Keith said:
It's called a discussion.

Well, maybe I am getting unduly sensitive, but I don't think so.
There is nothing wrong with a discussion, as long as it is clearly
not a reply to the OPs problem. About six words in a comment would
do it. Surely you don't disagree that your answer (which I didn't
snip - you did) had nothing to do with the OPs problem, and reading
it would only complicate life for him?
 
P

Peter Nilsson

CBFalconer said:
Well, maybe I am getting unduly sensitive,

Yes. You are. ;)
but I don't think so.
There is nothing wrong with a discussion, as long as it
is clearly not a reply to the OPs problem.

It was clearly a reply to your post.
About six words in a comment would
do it. Surely you don't disagree that your answer (which
I didn't snip - you did) had nothing to do with the OPs
problem,

Probably why Keith began with "True, but incomplete."
and reading it would only complicate life for him?

The OP is never the only stakeholder in a usenet thread.

Besides, value adding is not confusion. You might as well
tell Chris Torek to stop posting. He value adds to the max.
;)
 
K

Keith Thompson

CBFalconer said:
Well, maybe I am getting unduly sensitive, but I don't think so.
There is nothing wrong with a discussion, as long as it is clearly
not a reply to the OPs problem. About six words in a comment would
do it. Surely you don't disagree that your answer (which I didn't
snip - you did) had nothing to do with the OPs problem,
Agreed.

and reading
it would only complicate life for him?

I don't see how. (I could argue that giving him the impression that
the array-to-pointer conversion occurs only in the context of argument
passing might complicate life for him.)
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top