Re: variable length array
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Wed, 03 Jun 2009 17:05:23 -0700
On Wed, 03 Jun 2009 10:36:43 +0200, Jean-Claude Arbaut
<jeanclaudearbaut@xxxxxxxxx> wrote:
Barry Schwarz wrote:
On Wed, 03 Jun 2009 02:16:55 +0200, Jean-Claude Arbaut
<jeanclaudearbaut@xxxxxxxxx> wrote:
Hi,
I can use:
double get(int n, int p, double t[n][p], int i, int j) {
return t[i][j];
}
and in another function
double (*u)[];
I believe this would need to be
double (*u)[p];
as in examples 3 and 4 in 6.7.5.2(9)-(10)
There is one in n1256.pdf, p41 :-)
And I'm not a C standard expert, but I tought
it's legal to have one incomplete dimension.
The example on page 41 shows a pair of declarations, each of which is
partially incomplete. But as set, none of the incomplete parts remain
incomplete.
gcc -Wall -std=c99 -pedantic doesn't bark at me.
I know that doesn't prove much, but that's strange.
t = (double (*)[])calloc(n*p, sizeof(double));
Surely you meant u here, not t.
yes
The cast is unnecessary.
Someone just told me that on fr.clc ;-) Actually it's
a bad habit I got from using a bad compiler that gave
a warning for uncasted (mc)alloc. I can't remember
which compiler it was.
Furthermore,
all bits zero need not be the representation of 0.0. A frequently
recommended construct is
t = calloc(n, sizeof *t);
or the malloc "almost equivalent"
t malloc (n * sizeof *t);
That should have been
t = malloc(n*sizeof *t)
Does it guarantee that memory is zeroed ?
a=get(n,p,u,i,j);
Now, how would I declare u for 3 dimensions or more ?
When declaring arrays in functions and pointers to arrays, only the
high-order dimension can be unspecified/omitted.
Same remark as above.
If t3 is going to be defined in the function as
double t3[x][y][z]
then u3 will need to be defined as
double (*u3)[y][z];
Is there really no way to allocate it ?
I mean, it's possible with a warning to do
double *p = malloc(100*sizeof(double));
a = get(10,10,p,0,0);
This is not correct. Even if the compiler calls the diagnostic a
warning you still invoke undefined behavior. The function is
expecting a double(*)[p] and you are passing a double*. There is no
guarantee that the two pointers have the same representation or
alignment. Also there are several discussions in the archive if it is
legal to step through a 2D array as if it were a long 1D array.
But what should I do to declare it correctly ?
I showed the definition of the array and the pointer above. You
assign allocated memory to the pointer with
u3 = calloc(x, sizeof *u3);
or the malloc "almost equivalent"
u3 = malloc (x * sizeof *u3);
the same as I showed for a 2D pointer.
--
Remove del for email
.
- Follow-Ups:
- Re: variable length array
- From: Scarlet Pimpernel
- Re: variable length array
- References:
- variable length array
- From: Jean-Claude Arbaut
- Re: variable length array
- From: Barry Schwarz
- Re: variable length array
- From: Jean-Claude Arbaut
- variable length array
- Prev by Date: Re: Some language not implemented in C?
- Next by Date: Re: Whats the point of bool?
- Previous by thread: Re: variable length array
- Next by thread: Re: variable length array
- Index(es):
Relevant Pages
|