I must admit that the concept of multidimensional arrays and the use of pointers in C is giving me quite a headache in many of my projects.
Say, I want a 3-dimensional array with 3 entries in each dimension, i.e. 27 entries total. I want to run through the first dimension, and use curly brackets to initialize all entries in that dimension. But not using for loops. Something like this:
byte cube[3][3][3];
cube[0] = {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}};
cube[1] = {{1, 1, 1}, {0, 0, 1}, {0, 1, 0}};
cube[2] = {{0, 1, 1}, {1, 0, 0}, {0, 1, 1}};
Of course this will not work. But it shows the main idea of what I want to do. Initializing a 3-dimensional array using curly brackets and no for loops. How could I do this exactly?
byte cube[3][3][3] ={{{0, 0, 0}, {1, 0, 1}, {1, 1, 0}},{{1, 1, 1}, {0, 0, 1}, {0, 1, 0}},{{0, 1, 1}, {1, 0, 0}, {0, 1, 1}}};
Actually, I found out just the way to do it.
This is useful because I do not want to fill out all entries in my actual array. However, this poses a new problem for me.Code:byte cube[3][3][3] = {[0] = {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}}, [1] = {{1, 1, 1}, {0, 0, 1}, {0, 1, 0}}; [2] = {{0, 1, 1}, {1, 0, 0}, {0, 1, 1}}};
I apparently need to declare and initialize my array in the same line. I want to do this in a function in one C file. However, I need access to the array from another C file. How would I go about doing that, since the array is only declared in the function's scope?
You can pass a pointer to the array, but then you will need to use some form of loop to initialize it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks