Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > string buffer

Reply
Thread Tools

string buffer

 
 
George2
Guest
Posts: n/a
 
      12-10-2007
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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-10-2007
George2 wrote:
>
> 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.

--
pete
 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      12-10-2007
George2 schrieb:

> 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

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <4740ad67$0$3811$>
 
Reply With Quote
 
vicks
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 9:16 am, Johannes Bauer <dfnsonfsdu...@gmx.de> wrote:
> George2 schrieb:
>
> > 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
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      12-10-2007
vicks wrote:
> 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.
 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      12-10-2007
vicks schrieb:

> 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

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <4740ad67$0$3811$>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-10-2007
George2 wrote:
>
> 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.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2007
CBFalconer <> writes:
[...]
> 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).

--
Keith Thompson (The_Other_Keith) <kst->
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-12-2007
Keith Thompson wrote:
> CBFalconer <> writes:
> [...]
> > 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.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-12-2007
CBFalconer <> writes:
[SNIP]
> 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.

--
Keith Thompson (The_Other_Keith) <kst->
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
buffer creates only read-only buffer? Neal Becker Python 0 01-08-2009 01:58 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57