Thread: 3D array initialization in struct

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    3D array initialization in struct

    Hi All,

    I have a struct like this:

    Code:
    struct Cache
    {
        int *** cache;
    };
    and in my main() I have
    Code:
    int cache[8][1][1];
    struct Cache sim;
    
    sim.cache = (int ***)cache;
    I want to set the int *** cache to the same 3D array called "cache" in main. However, when I try something like "sim.cache[1][1][1] = 0;", I get a segfault.

    I don't know if I'm initializing the 3D array in my struct correctly. Can someone confirm please?

    Thanks,
    Colin
    Last edited by colinexl; 06-01-2007 at 06:19 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Pointer != array, etc.

    Have you tried something like this?
    Code:
    struct Cache
    {
        int (*cache)[1][1];
    };
    
    int cache[8][1][1] =
    {
       { {1} },
       { {2} },
       { {3} },
       { {4} },
       { {5} },
       { {6} },
       { {7} },
       { {8} },
    };
    
    int main(void)
    {
       struct Cache sim = {cache};
    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. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM