pointer to multidimensional array..

This is a discussion on pointer to multidimensional array.. within the C Programming forums, part of the General Programming Boards category; i have the following chunk of code.. Code: struct covar covariant[zeta_i][zeta_j][zeta_k]; struct covar ***p_covariant; **p_covariant=&covariant[0][0][0]; but it is giving seg ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    pointer to multidimensional array..

    i have the following chunk of code..

    Code:
    struct covar covariant[zeta_i][zeta_j][zeta_k];
    struct covar ***p_covariant;
    **p_covariant=&covariant[0][0][0];
    but it is giving seg fault while running..

    can someone plzz help me with this..

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Providing more code might be helpful. Is this what you might need?
    Code:
       struct covar covariant[zeta_i][zeta_j][zeta_k];
       struct covar (*p_covariant)[zeta_j][zeta_k] = covariant;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    hmm..i m a little confused.. in this case.. if i have to pass the array to another function can i just pass the pointer?

    i think this may work..
    thanks..

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something like this, perhaps?
    Code:
    void foo(struct covar p_covariant[zeta_i][zeta_j][zeta_k])
    {
    }
    
    int main(void)
    {
       struct covar covariant[zeta_i][zeta_j][zeta_k];
       /* ... */
       foo(covariant);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by MDofRockyView
    I'm not sure if this is correct, are you just trying to assign the pointer to the array? Does this work?
    Code:
    struct covar covariant[5][5][5];
    struct covar ***p_covar;
    p_covar = covariant;
    It's not the correct type.
    Type mismatch (assignment) (struct covar *** = struct covar (*)[5][5])
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    11
    @Dave_Sinkula:
    your suggestion works.. but doesnt it reduce the efficiency of program since you have to pass the an array to a function.. my program needs very high efficiency and hence i am looking for a better way..

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Arrays are never passed by value[*] since C can't; it is a pointer that is passed.

    Try not to prematurely optimize: if you really need efficiency, profile to find the bottlenecks.


    [*edit]Unless you consider the value of an array to be a pointer to the first element.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    MDofRockyView, read what Dave wrote - everything you've suggested is wrong in one way or another.

    @zoso123
    All these are the same as far as the compiler is concerned.
    Code:
    void foo(struct covar p_covariant[zeta_i][zeta_j][zeta_k]) {
    }
    void foo(struct covar p_covariant[][zeta_j][zeta_k]) {
    }
    void foo(struct covar (*p_covariant)[zeta_j][zeta_k]) {
    }
    All result in a single pointer being passed to the array, and all can be accessed using p_covariant[x][y][z] notation just as if the array were being accessed in main.

    None result in an entire array being copied.

    The first one has the advantage of being a simple copy/paste of the array you want to pass to the function in the first place, so it's dead easy to remember how to do it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by MDofRockyView
    How would you just assign a pointer to the 3d array? Is this incorrect?

    struct structype *p_struct = &array[0][0][0];
    That's a pointer to a single struct. This question has already been answered in this thread a couple times.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 09:29 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21