Making an array of 2D arrays in C


 
Thread Tools Search this Thread
Top Forums Programming Making an array of 2D arrays in C
# 1  
Old 08-16-2018
Making an array of 2D arrays in C

I hate I'm asking for help again. Unfortunately it seems there just aren't any links I can find on making an array that holds a bunch of two dimensional arrays. Maybe my google-fu is lacking. Basically I have a header file like this:

Code:
#define MATRIX 10

int white_l1[MATRIX][MATRIX];
int white_l2[MATRIX][MATRIX];
int white_l3[MATRIX][MATRIX];
int white_m1[MATRIX][MATRIX];
int white_m2[MATRIX][MATRIX];
int white_m3[MATRIX][MATRIX];
int white_r1[MATRIX][MATRIX];
int white_r2[MATRIX][MATRIX];
int white_r3[MATRIX][MATRIX];

int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3};

When I try to compile with it I get lots of the following:

Code:
mine.h:165:21: warning: initialization of ‘int *' from incompatible pointer type ‘int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*
                     ^~~~~~~~
mine.h:165:21: note: (near initialization for ‘side_one[0]')
mine.h:165:31: warning: initialization of ‘int *' from incompatible pointer type ‘int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*
                               ^~~~~~~~
mine.h:165:31: note: (near initialization for ‘side_one[1]')
mine.h:165:41: warning: initialization of ‘int *' from incompatible pointer type ‘int (*)[10]' [-Wincompatible-pointer-types]
 int *side_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3}; /*

... etc, etc

I've tried making these lines pointers:

Code:
int *white_r3[MATRIX][MATRIX];

Then I just get similar output at compile time:

Code:
mine.h:165:91: note: (near initialization for ‘side_one[7]')
mine.h:165:101: warning: initialization of ‘int *' from incompatible pointer type ‘int * (*)[10]' [-Wincompatible-pointer-types]
 e_one[] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3};

To be honest, I'm a little shaky on what ‘int * (*)[10]' even means. I would guess that a pointer to a pointer where one has been cast, but I'm probably wrong. At least I would think so from the code I used.

Any suggestions or insight much appreciated.
# 2  
Old 08-16-2018
int * isn't enough information for C to know what kind of array is being pointed to. That assumes a single integer or 1D array.

int *matrix[size][size] does not make pointers to 2d arrays -- it makes a 2d array of pointers.

Pointing to an array of known size takes this slightly wonky syntax:

int (*variablename[size][size]) [elements];

The stuff inside the red brackets defines the array being pointed to, the [elements] outside it defines the actual number of elements.

An example:

Code:
int main(void)
{
        int arr2d[2][2]={ {1,2},{3,4} };
        int arr2db[2][2]={ {5,6},{7,8} };

        int (*ptr[2][2])[2]= { arr2d, arr2db };
}

I thank @yifangt for pointing out to me this was even possible. C has a few weird corners.

Last edited by Corona688; 08-16-2018 at 05:16 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 08-17-2018
Thanks again Corona688. I used the following in the way you described and it compiled perfect!

Code:
int (*side_one[MATRIX][MATRIX])[MATRIX] = { white_l1, white_l2, white_l3, white_m1, white_m2, white_m3, white_r1, white_r2, white_r3};

I will marked this solved. Much appreciated.
This User Gave Thanks to Azrael For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies

2. Programming

Calling an array of arrays in C.

Corona688 was great in helping me learn how to create arrays that hold other two dimensional array here. Unfortunately I didn't think ask about how to implement or call them. Basically, I'm trying to call an array of two-dimensional arrays like this: declaration: int (*side_one) = { { white_l1,... (6 Replies)
Discussion started by: Azrael
6 Replies

3. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

4. Shell Programming and Scripting

perl script :making array

Hi , i have a perl script which takes 'csv; file as input parameter and performs some tasks. CSV file format : MAGENTF,AGENTF8,mahfr001,tksfr01,. ./.profile 1>/dev/null 2>&1;ps -fe|grep 'gn1avm_agent -n AGENTF8'|grep gn1avm_agent|tr -s '' ''|cut -d ' ' -f 3|xargs kill -9,,. ./.profile... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

5. Shell Programming and Scripting

using arrays and also help with array.contains functionality

here is what i have... i=1 while read line do if grep -i-q "create procedure"<<<$line then startline="$line" endline="blahblah" Get procedure name into a variable named procName procName="procedure name is stored" do some... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

7. Shell Programming and Scripting

Making array of string with bash

in.txt libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer-0_10-plugins-base Output should be: libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer0_10-plugins-base Then: #!/bin/sh v=(libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good... (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

Searching array of arrays in perl

Suppose there are two arrays of arrays: @A = ( , , , ); @B = ( , , , , ); For each of $A, $A, $A..., I want to find the corresponding one in @B (match the letter, like $A eq $B), and print out both the second item, for example, $A and $B. How can I do this in perl? grep + map? Hope I... (1 Reply)
Discussion started by: zx1106
1 Replies

9. Shell Programming and Scripting

Perl array of arrays

Hi, I am trying to assign an array as a value to one of the array element, I mean I have an array @KS and array @kr. I want array @KS to hold @kr as an element. So I am doin this $KS=@kr; But the value stored is number of elements in the @kr array. Can... (2 Replies)
Discussion started by: eamani_sun
2 Replies

10. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies
Login or Register to Ask a Question