Array Initialization problems.

Discussion in 'C Programming' started by Trevor Qi, May 14, 2004.

  1. Trevor Qi

    Trevor Qi Guest

    Hi,dear all:

    I have the following question. If I want to initialize an array using
    braces, is there a way that I can define them and give the initial
    value in different lines, like:

    int X[5];
    X[5] = {0,0,1,2,9};

    But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    works.
    I really want to do this because this is a public array and will be
    used by many Subroutines.

    Thanks!

    Sincerely,
    Trevor
     
    Trevor Qi, May 14, 2004
    #1
    1. Advertising

  2. Trevor Qi

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Trevor Qi wrote:

    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};


    Not unless you use a typographical break rather than a statement break.

    You'd have to do something like

    int X[5]
    = {0,0,1,2,9};

    Otherwise, what you want to do is not possible, and your code sample is
    incorrect.

    > But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    > works.
    > I really want to do this because this is a public array and will be
    > used by many Subroutines.
    >
    > Thanks!
    >
    > Sincerely,
    > Trevor



    - --

    Lew Pitcher, IT Consultant, Enterprise Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFApQjtagVFX4UWr64RAgQiAJ0XI+5JF4zrFcV5KOHw8rq2SnOgUQCg3qUf
    62gOL0i9VLRUrwkhV++G1zY=
    =Z3w1
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, May 14, 2004
    #2
    1. Advertising

  3. Trevor Qi wrote:
    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};
    >

    As I know, there isn't, even in C99.

    Explain any reaseon why you want to do it in two lines.

    vir
     
    Victor Nazarov, May 14, 2004
    #3
  4. (Trevor Qi) wrote in
    news::

    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};
    >
    > But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    > works.
    > I really want to do this because this is a public array and will be
    > used by many Subroutines.


    Then no, you cannot. You could do something silly like:

    x[0] = 0, x[1] = 0, x[2] = 1, x[3] = 2, x[4] = 9;

    but that's probably not something that will scale too well. If x[] is
    file-scoped you could create a function level array and the memcpy() it.
    E.g.

    #include <string.h>

    int x[5];

    int main(void)
    {
    int private_x[] = { 0, 0, 1, 2, 9 };

    memcpy(x, private_x, sizeof x);

    return 0;
    }

    --
    - Mark ->
    --
     
    Mark A. Odell, May 14, 2004
    #4
  5. Trevor Qi

    Tim Orbaker Guest

    Trevor Qi wrote:
    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};
    >
    > But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    > works.
    > I really want to do this because this is a public array and will be
    > used by many Subroutines.


    It sounds to me like this is what you want (tested with gcc):

    In a header named 'array.h', include the line:

    extern int X[5];

    Then, create another file (perhaps: array.c), include:

    int X[5] = { 0, 0, 1, 2, 9 };

    Then either #include 'array.h' in each file that uses this array or use:

    extern int X[5];

    in any lexical scope that uses the array.

    Tim
     
    Tim Orbaker, May 14, 2004
    #5
  6. In 'comp.lang.c', (Trevor Qi) wrote:

    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};
    >
    > But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    > works.
    > I really want to do this because this is a public array and will be
    > used by many Subroutines.


    If it is a read-only data, you can define it

    /* in a unique compile unit */
    static int const X[] = {0,0,1,2,9};

    or

    /* shared constant */
    int const X[];

    with this declaration:
    extern int const X[] = {0,0,1,2,9};

    Example :

    #include <stdio.h>

    extern int const X[] = {0,0,1,2,9};

    int const X[];

    int main (void)
    {
    int i;

    for (i = 0; i < sizeof X / sizeof *X; i++)
    {
    printf ("X[%d] = %d\n", i, X);
    }
    return 0;
    }

    D:\CLC\T\TREVOR>bc
    X[0] = 0
    X[1] = 0
    X[2] = 1
    X[3] = 2
    X[4] = 9

    Or

    /* const.c */
    #include "const.h"

    int const X[];

    #ifndef H_ED_CONST_20040514223101
    #define H_ED_CONST_20040514223101

    /* const.h */
    extern int const X[] = {0,0,1,2,9};

    #endif /* guard */

    /* Guards added by GUARD (c) ED 2000-2003 May 09 2004 Ver. 1.6 */

    /* main.c */
    #include <stdio.h>
    #include "const.h"

    int main (void)
    {
    int i;

    for (i = 0; i < sizeof X / sizeof *X; i++)
    {
    printf ("X[%d] = %d\n", i, X);
    }
    return 0;
    }

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, May 14, 2004
    #6
  7. Trevor Qi wrote:

    > Hi,dear all:
    >
    > I have the following question. If I want to initialize an array using
    > braces, is there a way that I can define them and give the initial
    > value in different lines, like:
    >
    > int X[5];
    > X[5] = {0,0,1,2,9};
    >
    > But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
    > works.
    > I really want to do this because this is a public array and will be
    > used by many Subroutines.


    Assignment of arrays is not part of C. To copy arrays at a go, use
    memmove() or memcpy().

    I have given another answer in the code below, but it will *not* work
    with C89 compilers, which means it will not work with most actually
    existing compilers. Even when it _does_ work, it may not be the best
    approach.

    int main(void)
    {
    int *X;
    X = (int [5]){ 0, 0, 1, 2, 9}; /* make the pointer (not array) X
    point to an anonymous array */
    return 0;
    }
     
    Martin Ambuhl, May 14, 2004
    #7
  8. On Fri, 14 May 2004 22:00:16 +0400, Victor Nazarov wrote:

    > Trevor Qi wrote:
    >> Hi,dear all:
    >>
    >> I have the following question. If I want to initialize an array using
    >> braces, is there a way that I can define them and give the initial
    >> value in different lines, like:
    >>
    >> int X[5];
    >> X[5] = {0,0,1,2,9};
    >>

    > As I know, there isn't, even in C99.


    Then you know wrong: C99 permits compound literals.

    Like so:

    int *X;
    X = (int [5]) {0,0,1,2,9};

    Not exactly the same, but damned close.

    >
    > Explain any reaseon why you want to do it in two lines.


    A lot of other languages permit much the same thing.

    >
    > vir


    --
    yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
    To email me, rot13 and convert spelled-out numbers to numeric form.
    "Makes hackers smile" makes hackers smile.
     
    August Derleth, May 16, 2004
    #8
  9. Trevor Qi

    Ben Pfaff Guest

    August Derleth <> writes:

    > On Fri, 14 May 2004 22:00:16 +0400, Victor Nazarov wrote:
    >
    >> Trevor Qi wrote:
    >>> Hi,dear all:
    >>>
    >>> I have the following question. If I want to initialize an array using
    >>> braces, is there a way that I can define them and give the initial
    >>> value in different lines, like:
    >>>
    >>> int X[5];
    >>> X[5] = {0,0,1,2,9};
    >>>

    >> As I know, there isn't, even in C99.

    >
    > Then you know wrong: C99 permits compound literals.
    >
    > Like so:
    >
    > int *X;
    > X = (int [5]) {0,0,1,2,9};
    >
    > Not exactly the same, but damned close.


    Even closer:
    int X[5];
    memcpy(X, (const int[5]) {0, 0, 1, 2, 9}, sizeof X);
    --
    "I hope, some day, to learn to read.
    It seems to be even harder than writing."
    --Richard Heathfield
     
    Ben Pfaff, May 16, 2004
    #9
  10. Trevor Qi

    Old Wolf Guest

    Martin Ambuhl <> wrote:
    > Trevor Qi wrote:
    > > I have the following question. If I want to initialize an array using
    > > braces, is there a way that I can define them and give the initial
    > > value in different lines, like:
    > >
    > > int X[5];
    > > X[5] = {0,0,1,2,9};
    > >
    > > But this gives me error message.

    >
    > Assignment of arrays is not part of C. To copy arrays at a go, use
    > memmove() or memcpy().
    >
    > int *X;
    > X = (int [5]){ 0, 0, 1, 2, 9};


    Is it completely portable to go:

    int X[5];
    memcpy(X, (int []){ 0, 0, 1, 2, 9}, 5 * sizeof(int));

    (I imagine that a trap representation might be generated after copying
    some of the bytes of an int)
     
    Old Wolf, May 16, 2004
    #10
  11. Trevor Qi

    Ben Pfaff Guest

    (Old Wolf) writes:

    > Is it completely portable to go:
    >
    > int X[5];
    > memcpy(X, (int []){ 0, 0, 1, 2, 9}, 5 * sizeof(int));


    It's portable, but only to C99 implementations, which severely
    undermines its practical value.

    > (I imagine that a trap representation might be generated after copying
    > some of the bytes of an int)


    No, objects may always be safely manipulated as arrays of
    character type, and this is the way that memcpy copies.
    --
    Here's a tip: null pointers don't have to be *dull* pointers!
     
    Ben Pfaff, May 17, 2004
    #11
    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. JKop
    Replies:
    10
    Views:
    960
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    457
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    471
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    943
    Victor Bazarov
    Sep 28, 2006
  5. aaragon
    Replies:
    2
    Views:
    626
    James Kanze
    Nov 2, 2008
Loading...

Share This Page