Question : 3d array with pointer to pointer.

Discussion in 'C Programming' started by Joonshik Kim, Nov 12, 2006.

  1. Joonshik Kim

    Joonshik Kim Guest

    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 = d[i-1] + ny;
    for(i = 0; i < nx ; i++)
    for(j = 1; j < ny ; j++) d[j] = d[j-1] + nz;


    for(i=0 ; i < nx; i ++)
    for(j = 0; j < ny; j++)
    for(k = 0 ; k < nz; k++)
    d[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.
     
    Joonshik Kim, Nov 12, 2006
    #1
    1. Advertising

  2. 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 = malloc(ny * sizeof *d);
    if(d == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }

    for(j = 0; j < ny; j++)
    {
    d[j] = malloc(nz * sizeof *d[j]);
    if(d[j] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
    for(k = 0; k < nz; k++)
    {
    d[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.
     
    Richard Heathfield, Nov 12, 2006
    #2
    1. Advertising

  3. Joonshik Kim

    Joonshik Kim Guest

    Oh..Thank you..
    It works nice..

    "Richard Heathfield" <> wrote in message
    news:p...
    > 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 = malloc(ny * sizeof *d);
    > if(d == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
    >
    > for(j = 0; j < ny; j++)
    > {
    > d[j] = malloc(nz * sizeof *d[j]);
    > if(d[j] == NULL) { CLEAN_UP; HANDLE_THE_ERROR; }
    > for(k = 0; k < nz; k++)
    > {
    > d[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.
     
    Joonshik Kim, Nov 12, 2006
    #3
  4. Joonshik Kim

    Guest

    [Horribly OT -- Richard Heathfield please read urgently] Was: Re: Question : 3d array with pointer to pointer.

    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)
     
    , Nov 15, 2006
    #4
  5. Joonshik Kim

    Guest

    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 = d[i-1] + ny;
    > for(i = 0; i < nx ; i++)
    > for(j = 1; j < ny ; j++) d[j] = d[j-1] + nz;


    You now try to access 60 addresses. What is wrong with this picture?
     
    , Nov 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    349
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    676
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,266
  4. Hansen
    Replies:
    3
    Views:
    1,108
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    458
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page