3D array initialization in struct

This is a discussion on 3D array initialization in struct within the C Programming forums, part of the General Programming Boards category; Hi All, I have a struct like this: Code: struct Cache { int *** cache; }; and in my main() ...

  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 07:19 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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, 03:14 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10: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, 02:34 PM

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