How to declare a dynamic array

Discussion in 'C Programming' started by Linda, Jun 29, 2004.

  1. Linda

    Linda Guest

    How can I declare a two dimention array of character with dynamical
    first dimention and static second dimention using mix of pointer* and
    []. I think "char *A[4]" means first dimention is static and second
    dimension is dynamical. Thank you.
    Linda, Jun 29, 2004
    #1
    1. Advertising

  2. In 'comp.lang.c', (Linda) wrote:

    > How can I declare a two dimention array of character with dynamical
    > first dimention and static second dimention using mix of pointer* and
    > []. I think "char *A[4]" means first dimention is static and second
    > dimension is dynamical. Thank you.


    char (*A)[4] = malloc (sizeof *A * nb_of_elements);

    --
    -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, Jun 29, 2004
    #2
    1. Advertising

  3. Linda

    Malcolm Guest

    "Linda" <> wrote
    >
    > How can I declare a two dimention array of character with dynamical
    > first dimention and static second dimention using mix of pointer* and
    > []. I think "char *A[4]" means first dimention is static and second
    > dimension is dynamical. Thank you.
    >

    char *A[4];

    is the same as

    char* A[4];

    which you will see is an array of 4 character pointers.

    char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in C,
    beyond the very simplest declaration, are horrible.
    What most programmers would do is declare

    char *A;

    and just manage the second dimension by hand. You see this quite commonly in
    24 bit rgb images, for example.
    Malcolm, Jun 29, 2004
    #3
  4. Malcolm <> spoke thus:

    > char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in C,
    > beyond the very simplest declaration, are horrible.
    > What most programmers would do is declare


    > char *A;


    Is the syntax really that bad? I would think your original
    declaration would do a better job of conveying to an informed reader
    that you have a dynamic array of 4 byte arrays...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jun 30, 2004
    #4
  5. Linda

    Malcolm Guest

    "Christopher Benson-Manica" <> wrote
    > Malcolm <> spoke thus:
    >
    > > char (*A)[4] is what you want, but this syntax is horrible. 2d arrays in
    > > C, beyond the very simplest declaration, are horrible.
    > > What most programmers would do is declare

    >
    > > char *A;

    >
    > Is the syntax really that bad? I would think your original
    > declaration would do a better job of conveying to an informed reader
    > that you have a dynamic array of 4 byte arrays...
    >

    Yes it is. If you follow the ng you will see that there is a steady trickle
    of newbies hopelessly confused by C's multi-dimensional arrays, largely
    because most introductory textbooks introduce the 2d array at the same time
    as the 1d array.
    char (*A)[4] is gibberish. No-one should be expected to follow such a
    construct, or use it in real code. Even the informed reader, who can work
    out what it means, will find it hard to read and make mistakes.
    Malcolm, Jun 30, 2004
    #5
    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. George Ter-Saakov

    how do i declare const array?

    George Ter-Saakov, Aug 22, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,622
    George Ter-Saakov
    Aug 22, 2003
  2. Philipp
    Replies:
    5
    Views:
    6,444
    Ron Natalie
    Oct 16, 2003
  3. Bob Smith
    Replies:
    2
    Views:
    2,709
    Jeff Schwab
    Jan 1, 2004
  4. Dylan Nicholson

    Declare an array parameter to be const?

    Dylan Nicholson, Jan 27, 2005, in forum: C++
    Replies:
    6
    Views:
    5,200
    Andrey Tarasevich
    Jan 27, 2005
  5. Geetesh
    Replies:
    19
    Views:
    602
    Yakov Lerner
    Mar 3, 2004
Loading...

Share This Page