Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to declare a dynamic array

Reply
Thread Tools

How to declare a dynamic array

 
 
Linda
Guest
Posts: n/a
 
      06-29-2004
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.
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-29-2004
In 'comp.lang.c', http://www.velocityreviews.com/forums/(E-Mail Removed) (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/
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      06-29-2004

"Linda" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      06-30-2004
Malcolm <(E-Mail Removed)> 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.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-30-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote
> Malcolm <(E-Mail Removed)> 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.


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
which is the better way to declare dynamic single dimension array inside struct Geetesh C Programming 19 03-03-2004 03:34 PM
how to declare array of class pointer Bob Smith C++ 2 01-01-2004 04:09 PM
declare array of objects in header file Philipp C++ 5 10-16-2003 06:08 PM
how do i declare const array? George Ter-Saakov ASP .Net 4 08-22-2003 04:41 PM



Advertisments