Reputation:
For illustration,two dimensional pointer arrays in C like int* x[][4]
can be declared ,where first index can be fixed at run time that it is dynamic then can i say that dynamic declaration of dynamic one-dimensional array can be achieved in someway by replacing the second index to value 1 like int*[][1]
and for two dimensional array dynamic two-dimensional array by int*[][][1]
Does calloc()
malloc()
internally use them is my logic correct at all and this applied that is in a simplistic case of dynamic array declaration use inside a function in C language program
Upvotes: 0
Views: 87
Reputation: 181714
For illustration,two dimensional pointer arrays in C like
int* x[][4]
can be declared
Only in two cases:
when an initializer for x
implicitly conveys the value of the first dimension,
int* x[][4] = { {NULL, NULL, NULL, NULL}, {NULL, NULL, NULL, NULL} };
as the type of a function parameter. In that case, it is exactly equivalent to declaring the parameter to have type int *(*)[4]
: pointer to array of four pointers to int
. And that would also be the case if the first dimension were actually given.
void foo(int* x[][4]);
,where first index can be fixed at run time
In case (1) above, no, the first dimension is determined at compile time.
In case (2) above, not exactly. It is then the declaration of a pointer to a 1D array. Such a pointer can point to the first element of a 2D array of int *
, and it can be indexed as if it in fact designated such a 2D array. The size of the first dimension is not conveyed, but if the pointer in fact points to an object then that dimension is fixed for the function's purposes, whether the pointed-to object is dynamically allocated or not.
that it is dynamic then can i say that dynamic declaration of dynamic one-dimensional array can be achieved in someway by replacing the second index to value 1 like int*[][1]
That change produces a statically different type (in those contexts where it is valid at all). In case (2), but not case (1), both this type and the one we were discussing previously can point to a dynamically-allocated object, but they can also point to an automatic- or static-duration object of appropriate type. There's nothing inherently dynamic there.
and for two dimensional array dynamic two-dimensional array by int*[][][1]
No. Not even for the type of a function parameter. At most the (one) leading dimension can be omitted.
Does calloc() malloc() internally use them
Not at all. How could they? The allocation functions do not see this datatype at all. You tell them only how much space to allocate, either as a single composite value (malloc
) or as an (element count, element size) pair (calloc
). Thus the caller needs to know how much memory is needed. And this in any event applies only in case (2). Case (1) inherently produces static or automatic allocation, not dynamic.
is my logic correct at all
No.
and this applied that is in a simplistic case of dynamic array declaration use inside a function in C language program
Neither as a local variable of a function (case 1) nor as a function parameter (case 2).
Upvotes: 1