简体   繁体   中英

How to declare an 3D array with different Datatypes and different sizes in C?

I need to declare an array (in c) which holds two 2D arrays and one 1D array with different size and different type in it.

Unfortunately my google search not really helped...

int ram [128][64];

int chk_ram [128][64];

char arr_block[8];

Is it possible to pack these arrays in one big array?

@unwind: Here is my Function, it was originally a Python Function but because it was to slow i am now Trying to process these Arrays in C, because i expect that it´s much faster. The Function should act like a Blackbox i put 3 Arrays in and then there come 3 Arrays out which are going back to Python.

Here is the C Function (I think there are some mistakes in it):

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int test(int **ram,int n_ram,int **chk_ram, int n_chk_ram,int **arr_block,int n_arr_block){
    int i,j,k;
    int yDog,p,x,d,y,z;
    int *args_arr = (int*)malloc(size*sizeOf(int));
    int *dog = (int*)malloc(size*sizeOf(int));

    for (yDog=0;yDog<=8;yDog++){
        p=yDog*8;
        line='' ?? /* ?? */
        for (x=0;x<=128;x++){
            d=0;

            if (chk_ram[(int)(x/16),yDog] == 1){
                if (x%16 == 0){
                    arr_block[(int)(x/16),yDog] = ''; /* ?? */
                }
                for (y=0;y<=8;y++){
                    z = pow(2,y)
                    d += ram[x,p+y]*z;
                }

                arr_block[(int)(x/16),yDog] += chr(d); /* ?? */
                if ((x+1)%16 == 0 && x){
                    chk_ram[(int)(x/16),yDog] = 0;
                    line += arr_block[(int)(x/16),yDog]; /* ?? */
                }
            }
            else{
                if ((x+1)%16 == 0 && x){
                    chk_ram[(int)(x/16),yDog] = 0;
                    line += arr_block[(int)(x/16),yDog];
                    x += 1;
                }
                else{
                    x+=15;
                }
            }

        }
    }
    dog[yDog] = line; /* ?? */

    args_arr = {ram, chk_ram, arr_block)
    return args_arr;
}

I work with Ctypes if someone know this :)

Why using an array when you can use structure ?

struct foobar {
  int ram [128][64];
  int chk_ram [128][64];
  char arr_block[8];
}

Or, with a typedef:

typedef struct {
  int ram [128][64];
  int chk_ram [128][64];
  char arr_block[8];
} Foobar;

In my understanding of your question, you are trying to mix array of different kind and length which is error prone (at least in C).

If you need to return that structure from a function, you can do:

struct foobar myFunction(int c) { /* or Foobar */
  struct foobar val;
  /* stuff with val */
  return val;
}

Or:

void myFunction(struct foobar *val, int c) { /* or Foobar */
  val->ram[0][0] = c;
}

int main(void) {
  struct foobar val;
  myFunction(&val, c);
}

I would definitely go for the use of pointer (second function with struct foobar* ) because otherwise you would copy the whole structure and it will be slower than the pointer version.

And if your function need to initialize your structure, and if you do more work with it, perhaps it would be better to use malloc / free to avoid unwanted copies.

In general you can not store different types of objects in an array in C. Please note that an array of int for example is a "type".

What I would recommend for your case and any other case of grouping stuff together is to use a structure.

You can also check this question for more details.

Is it possible to pack these arrays in one big array?

No you can't, as all elements of an array will have the same type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM