string buffer

Discussion in 'C Programming' started by George2, Dec 10, 2007.

  1. George2

    George2 Guest

    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
    George2, Dec 10, 2007
    #1
    1. Advertising

  2. George2

    pete Guest

    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
    pete, Dec 10, 2007
    #2
    1. Advertising

  3. 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$>
    Johannes Bauer, Dec 10, 2007
    #3
  4. George2

    vicks Guest

    On Dec 10, 9:16 am, Johannes Bauer <> 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
    vicks, Dec 10, 2007
    #4
  5. George2

    Mark Bluemel Guest

    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.
    Mark Bluemel, Dec 10, 2007
    #5
  6. 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$>
    Johannes Bauer, Dec 10, 2007
    #6
  7. George2

    CBFalconer Guest

    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
    CBFalconer, Dec 10, 2007
    #7
  8. 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) <>
    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"
    Keith Thompson, Dec 11, 2007
    #8
  9. George2

    CBFalconer Guest

    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
    CBFalconer, Dec 12, 2007
    #9
  10. 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) <>
    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"
    Keith Thompson, Dec 12, 2007
    #10
  11. George2

    CBFalconer Guest

    Keith Thompson wrote:
    > 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.


    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?

    --
    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
    CBFalconer, Dec 13, 2007
    #11
  12. CBFalconer <> wrote:
    > Keith Thompson wrote:
    > > CBFalconer <> writes:
    > > > 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.

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

    --
    Peter
    Peter Nilsson, Dec 13, 2007
    #12
  13. CBFalconer <> writes:
    > Keith Thompson wrote:
    >> 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.

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

    --
    Keith Thompson (The_Other_Keith) <>
    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"
    Keith Thompson, Dec 13, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun Kumar Srinivasan
    Replies:
    2
    Views:
    11,403
    Gregory A. Swarthout
    Jan 29, 2004
  2. Raja
    Replies:
    12
    Views:
    24,394
    John Harrison
    Jun 21, 2004
  3. Replies:
    2
    Views:
    603
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    412
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    274
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page