Barry said:
(e-mail address removed) wrote On 05/30/06 12:46,:
Okay i havent written that correctly.There arent 2 arrays.I just want
to declare a dynamic array A[31][130][256][256] and access it with
indices like i would a static array.
SomeType (*A)[31][130][256][256];
A = malloc(sizeof *A);
if (A == NULL)
abort();
A[1][2][3][4] = 42;
I'm sure you really meant
(*A)[1][2][3][4] = 42;
this is all good but guys i need to access the arrays like
a static array and not with pointer referencing
like i need to access A with A[][][][].Please is there someone who can
suggest a technique.there have been many qs asked as to why i need the
array ,what about the memory,FAQs i need to read.I thought i can get
some real help here.Is there no one who can actually solve the
problem.Why is there a need to give an alternate solution.I dont
require alternates.This is the problem plain and simple i have a to
declare a 4 dimensional dynamic array and need to then aces it like a
static array with normal array indices like a[][][][] and not
*A(i*j*k).I know the compiler treats it the same .But i need the access
to it through A[][][][].Please anyone.I would really appreciate a
solution then some condesecnding remarks or an alterante solution.
Please don't top-post.
Now, if I understand you right, you want to allocate a block of dynamic
memory such that you can use subscripts similar to four-dimensional
arrays to access the elements of the block... ?
The following code is one way I thought of. It's very inefficient and
importantly doesn't free() the allocated memory before program exit.
However it shows how you can use pointers to allocate a block of
dynamic memory and access it's elements *as if* it were a
four-dimensional static array.
Generally a N dimensional array of pointers of type T can provide
access to a N+1 dimensional array of objects of type *T. So, to access
a block of dynamic memory as a four-dimensional array, we must allocate
a three-dimensional array of pointers of the appropriate type. This
three-dimensional array of pointers can be accessed by a
two-dimensional array of pointers-to-pointers. This can in turn be
accessed by a one-dimensional array of
pointers-to-pointers-to-pointers. This is pointed to by a single
pointer-to-a-pointer-to-a-pointer-to-a-pointer.
/* Dynamically allocating a four dimensional array and using it */
#include <stdio.h>
#include <stdlib.h>
#define ARR_SIZ 10
int main(void) {
char ****array = NULL;
int cnt, cnt1, cnt2, cnt3;
if((array = malloc(ARR_SIZ * sizeof *array)) == NULL)
return EXIT_FAILURE;
for(cnt = 0; cnt < ARR_SIZ; ++cnt) {
if((array[cnt] = malloc(ARR_SIZ * sizeof **array)) == NULL)
return EXIT_FAILURE;
}
for(cnt = 0; cnt < ARR_SIZ; ++cnt) {
for(cnt1 = 0; cnt1 < ARR_SIZ; ++cnt1) {
if((array[cnt][cnt1] =
malloc(ARR_SIZ * sizeof ***array)) == NULL)
return EXIT_FAILURE;
}
}
for(cnt = 0; cnt < ARR_SIZ; ++cnt) {
for(cnt1 = 0; cnt1 < ARR_SIZ; ++cnt1) {
for(cnt2 = 0; cnt2 < ARR_SIZ; ++cnt2) {
if((array[cnt][cnt1][cnt2] =
malloc(ARR_SIZ * sizeof ****array)) ==
NULL)
return EXIT_FAILURE;
}
}
}
puts("Storing values in the array...");
for(cnt = 0; cnt < ARR_SIZ; ++cnt) {
for(cnt1 = 0; cnt1 < ARR_SIZ; ++cnt1) {
for(cnt2 = 0; cnt2 < ARR_SIZ; ++cnt2) {
for(cnt3 = 0; cnt3 < ARR_SIZ; ++cnt3)
array[cnt][cnt1][cnt2][cnt3] = cnt3;
}
}
}
puts("Printing out the array...");
for(cnt = 0; cnt < ARR_SIZ; ++cnt) {
for(cnt1 = 0; cnt1 < ARR_SIZ; ++cnt1) {
for(cnt2 = 0; cnt2 < ARR_SIZ; ++cnt2) {
for(cnt3 = 0; cnt3 < ARR_SIZ; ++cnt3) {
printf("array[%d][%d][%d][%d] = %d\n",
cnt, cnt1, cnt2, cnt3,
array[cnt][cnt1][cnt2][cnt3]);
}
}
}
}
exit(0);
}