Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question : 3d array with pointer to pointer.

Reply
Thread Tools

Question : 3d array with pointer to pointer.

 
 
Joonshik Kim
Guest
Posts: n/a
 
      11-12-2006
I was trying to define 3d array with pointer to pointer. I wrote like
following.

int ***d;
nx = 3;
ny = 5;
nz = 4;

d = (int ***)malloc((int) nx*sizeof(int **));
*d = (int **)malloc((int) nx*ny*sizeof(int*));
**d = (int *)malloc((int) nx*ny*nz*sizeof(int));
for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
for(i = 0; i < nx ; i++)
for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;


for(i=0 ; i < nx; i ++)
for(j = 0; j < ny; j++)
for(k = 0 ; k < nz; k++)
d[i][j][k] = 100*(i+1) + 10*(j+1) + k;


it compiled ok. but when I ran it, it shows error message like
"segementation fault.."

What did I do wrong ? can someone help me ?

thank you.



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-12-2006
Joonshik Kim said:

> I was trying to define 3d array with pointer to pointer. I wrote like
> following.
>
> int ***d;
> nx = 3;
> ny = 5;
> nz = 4;


d = malloc(nx * sizeof *d);
if(d == NULL) { HANDLE_THE_ERROR; }

for(i = 0; i < nx; i++)
{
d[i] = malloc(ny * sizeof *d[i]);
if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }

for(j = 0; j < ny; j++)
{
d[i][j] = malloc(nz * sizeof *d[i][j]);
if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
for(k = 0; k < nz; k++)
{
d[i][j][k] = 42;


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
Reply With Quote
 
 
 
 
Joonshik Kim
Guest
Posts: n/a
 
      11-12-2006
Oh..Thank you..
It works nice..

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Joonshik Kim said:
>
> > I was trying to define 3d array with pointer to pointer. I wrote like
> > following.
> >
> > int ***d;
> > nx = 3;
> > ny = 5;
> > nz = 4;

>
> d = malloc(nx * sizeof *d);
> if(d == NULL) { HANDLE_THE_ERROR; }
>
> for(i = 0; i < nx; i++)
> {
> d[i] = malloc(ny * sizeof *d[i]);
> if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
>
> for(j = 0; j < ny; j++)
> {
> d[i][j] = malloc(nz * sizeof *d[i][j]);
> if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
> for(k = 0; k < nz; k++)
> {
> d[i][j][k] = 42;
>
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999
> http://www.cpax.org.uk
> email: normal service will be restored as soon as possible. Please do not
> adjust your email clients.



 
Reply With Quote
 
plodger@gmail.com
Guest
Posts: n/a
 
      11-15-2006
Richard Heathfield wrote:
[snip]
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999
> http://www.cpax.org.uk
> email: normal service will be restored as soon as possible. Please do not
> adjust your email clients.


Hi Richard, it's Sarah Thompson here. I've been trying to contact you
urgently, but haven't succeeded through usual channels, so many
apologies for posting here but I am not aware of any alternative.

You need to contact your brother Robert urgently in connection with a
family matter. Feel free to email me directly and I'll fill you in on
the details.

Best regards,
Sarah (thompson a t email dt arc dt nasa d t gov)

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      11-15-2006

Joonshik Kim wrote:
> I was trying to define 3d array with pointer to pointer. I wrote like
> following.
>
> int ***d;
> nx = 3;
> ny = 5;
> nz = 4;
>
> d = (int ***)malloc((int) nx*sizeof(int **));


You store one address into d.

> *d = (int **)malloc((int) nx*ny*sizeof(int*));


You store one address at *d.

> **d = (int *)malloc((int) nx*ny*nz*sizeof(int));


You store one address at **d.

> for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
> for(i = 0; i < nx ; i++)
> for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;


You now try to access 60 addresses. What is wrong with this picture?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments