Help is need immidiately

V

vinod.bhavnani

Hello all,

I need desperate help

Here is the problem:

My problem today is with multidimensional arrays.

Lets say i have an array A[31][130][256][256] this is a 4 dimensional
static array.

How will i declare this same array with same dimensions dynamically and
also after dynamic declaration i want to be able to access contents of
array with indiceslike i should be able to access an elemt after after
dynamic declarartion as
A[23][34][56][78].Is this possible

Please help

Now pls remember that memory space for the array is not a concern and i
cant change this data structure

Thanks
 
R

Robert Gamble

Hello all,

I need desperate help

Here is the problem:

My problem today is with multidimensional arrays.

Lets say i have an array A[31][130][256][256] this is a 4 dimensional
static array.

How will i declare this same array with same dimensions dynamically and
also after dynamic declaration i want to be able to access contents of
array with indiceslike i should be able to access an elemt after after
dynamic declarartion as
A[23][34][56][78].Is this possible

Please help

Now pls remember that memory space for the array is not a concern and i
cant change this data structure

This is an FAQ question. The FAQ can be found at <http://c-faq.com/>,
your question is 6.16. If after reading the answer and trying to
implement the solution to fit your needs you have a specific question,
come back with what you have so far and a clear description of where
your problem lies.

Robert Gamble
 
W

Walter Roberson

I need desperate help
Now pls remember that memory space for the array is not a concern and i
cant change this data structure

We generally find that people when people don't leave homework
to the last minute, that they don't need desperate immediate help.

Here is the problem:
My problem today is with multidimensional arrays.
Lets say i have an array A[31][130][256][256] this is a 4 dimensional
static array.
How will i declare this same array with same dimensions dynamically

If it is the 'same array' then it would have the same properties
of being static and with those exact fixed dimensions. It would be
a contradiction in the question to ask to allocate a static array
dynamically, or to allocate an array with exact fixed dimensions
with variable dimensions.
 
V

vinod.bhavnani

Hi,

After reading the stuff i still have no idea as to how u would proceed
with a 4 dimensional array.I would really appreiciate it if someone who
actually knows how to assign a dynamic 4-D array in C could please help
me out.

Thanks
 
V

vinod.bhavnani

Hey,

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.

Thanks
 
E

Eric Sosman

Hey,

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;
 
D

Default User

Hi,

After reading the stuff i still have no idea as to how u would proceed
with a 4 dimensional array.I would really appreiciate it if someone
who actually knows how to assign a dynamic 4-D array in C could
please help me out.

Please review the .sig information at the end of this message.

What about the FAQ answer confuses you? You haven't explained why you
are having problems. What have you tried? Have you created a 2-D
dynamic array?

What do you want a dynamic array? Normally that is for when you don't
know the size of the dimensions. This problem smells like homework. You
haven't presented the overall problem, just "how do I do a dynamic 4-D
array." You haven't said WHY you want such a beast, nor have you said
what you mean by such an array.



Brian
 
B

Barry Schwarz

(e-mail address removed) wrote On 05/30/06 12:46,:
Hey,

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;


Remove del for email
 
V

vinod.bhavnani

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.

Thanks,
Barry said:
(e-mail address removed) wrote On 05/30/06 12:46,:
Hey,

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;


Remove del for email
 
E

Eric Sosman

Barry Schwarz wrote On 05/30/06 20:48,:
(e-mail address removed) wrote On 05/30/06 12:46,:
Hey,

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;

Good catch, but wrong diagnosis. What I *really*
meant was (ahem, ahem):

SomeType (*A)[130][256][256];
A = malloc(31 * sizeof *A);
if (A == NULL)
abort();
A[1][2[3][4] = 42;

(I guess you can tell who doesn't recall the last time
he used a 4D array ... Thanks for the eagle eye.)
 
K

Keith Thompson

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.

Don't top-post. Read <http://www.caliburn.nl/topposting.html>.

What are you really trying to accomplish? If this is a homework
assignment, we're not going to do it for you (unless you give us your
instructor's e-mail address so we can submit the solution directly).

There should be enough information in the FAQ for you to figure out
how to solve the problem. What exactly is giving you trouble?
 
W

W Marsh

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.

Don't top-post. Read <http://www.caliburn.nl/topposting.html>.

What are you really trying to accomplish? If this is a homework
assignment, we're not going to do it for you (unless you give us your
instructor's e-mail address so we can submit the solution directly).

There should be enough information in the FAQ for you to figure out
how to solve the problem. What exactly is giving you trouble?

He may have a purely academic interest in seeing how a C++ expert
might express this. I know I do - I don't know how this would be done
(chaining?), and I would quite like to see some approaches.
 
R

Richard Heathfield

W Marsh said:
He may have a purely academic interest in seeing how a C++ expert
might express this.

I presume you mean "C expert".
I know I do - I don't know how this would be done
(chaining?), and I would quite like to see some approaches.

Well, the FAQ covers it, pretty much.

Look, let's say you want a 1-D array of T. How would you do it? Like this?

#include <stdlib.h>

#include "t.h"

T *create_T_array(size_t z)
{
T *new = malloc(z * sizeof *new);
if(new != NULL)
{
size_t i = 0;
T blank = {0};
for(i = 0; i < z; i++)
{
new = blank;
}
}
return new;
}

void destroy_T_array(T **old)
{
if(old != NULL && *old != NULL)
{
free(*old);
*old = NULL;
}
}

So - how would you create an array of such arrays?

T **create_TT_array(size_t y, size_t z)
{
T **new = malloc(y * sizeof *new);
if(new != NULL)
{
size_t i = 0;
int ok = 1;
for(i = 0; ok && i < y; i++)
{
new = create_T_array(z);
if(new == NULL)
{
ok = 0;
while(i > 0)
{
--i;
destroy_T_array(new + i);
}
free(new);
new = NULL;
}
}
}

return new;
}

And so on and so forth. You build up from inside, creating the functions
that you will need in the "outer" layers. It's very simple.
 
A

Al Balmer

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.

That's not what this group is for. We'll be glad to help, but not do
your work for you. The relationship between pointers and array indexes
is very basic in C, and you should consult your textbooks. Try writing
something to do the job. If you can't make it work, post your code
here and ask for help.

If you really need someone else to do it for you, try one of the
*.sources.wanted groups.
 
E

Eric Sosman

Walter said:
Good catch, but wrong diagnosis. What I *really*
meant was (ahem, ahem):

SomeType (*A)[130][256][256];
A = malloc(31 * sizeof *A);
if (A == NULL)
abort();
A[1][2[3][4] = 42;


missing ] in line 42 ;-)

<Sigh.> Some days, it just doesn't pay to get out of bed.
 
S

santosh

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);
}
 
V

vinod.bhavnani

Hey,

Thank you santosh.Its been a pleasure.Someone finally who actually
knows how to solve the problem.Brilliant mate.No condescending
remarks..no pretences of actually "knowing" how to do it.No alternate
solution.Just plain and simple code.

Thanks
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);
}
 
K

Keith Thompson

Thank you santosh.Its been a pleasure.Someone finally who actually
knows how to solve the problem.Brilliant mate.No condescending
remarks..no pretences of actually "knowing" how to do it.No alternate
solution.Just plain and simple code.
[...]

Please stop top-posting. See <http://www.caliburn.nl/topposting.html>.

You got a lot of negative feedback on your original question. The
reason for this is that you stated a problem and demanded that we
write the code to solve it for you. We tried to point you to a
resource (the FAQ) that should have helped you solve the problem
yourself. You never explained just why you need to do this.

If this was homework, the point is for you to learn how to do it
yourself. If the rest of us are going to write all the code, why is
your participation necessary?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top