Re: variable length array



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
.



Relevant Pages

  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... It also returned the value of the pointer. ...
    (microsoft.public.vc.language)
  • Re: Arrays of incomplete type
    ... Is GCC 4 correct in reporting an array of incomplete type as an error? ... dereference a pointer to an incomplete type (the pointer is ... Opaque types allow data hiding and encapsulation: ...
    (comp.std.c)
  • Re: Inconsistencies
    ... sizeofworked in someFunction as it does in main - so you could ... C allows you to declare what looks like an array parameter, ... really a pointer parameter. ...
    (comp.lang.c)
  • Re: Passing an array of structuresfrom a pointer?
    ... an array of struct. ... You cannot assign a pointer to struct to a long* ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)
  • Re: Function pointer as function parameter
    ... Not as far as any standard-conforming compiler is concerned. ... expression, it "decays" to a pointer, so ... you declare subprogram formals with a different syntax which ... pointer to T, and when you write an array in a call ...
    (comp.lang.fortran)