0

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 :)

8
  • 3
    An array of structures? Commented Aug 25, 2014 at 13:27
  • typedef struct { int ram[128][64]; int chk_ram [128][64]; char arr_block[8]; } MYARRAY; Commented Aug 25, 2014 at 13:30
  • I dont know what you mean with "structures" but i think the Problem with this is that the Array should hold different Data Types so for me it seems impossible, because which Data Type has the Big Array which hold the 3 Arrays above? I need this to return all Arrays from a Function (This Function can only return 1 integer Array)... So a little bit confusing for me^^ Commented Aug 25, 2014 at 13:33
  • @user2127024 You should show the declaration of the function you're trying to use. In general you can't actually return an array in C, so it's a bit interesting to see what that code looks like. Perhaps it can help us understand what you're trying to do better. Commented Aug 25, 2014 at 13:37
  • @user2127024 You can't have a multidimensional array with different types, and you cannot return an array from a function directly. Instead place your 3 arrays inside a struct. That way, you can also return such a struct from a function. Commented Aug 25, 2014 at 13:38

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I assume that it would be better (algorithmically, stylistically) to have an array of structures with one ram and chk_ram each, instead of a structure of arrays.
@NoDataFound: Ok thank´s I understand what you mean. Is it also Possible to make a Function in C which has the type "Structure" instead of int? Because i also have different Data-Types of Parameter which i give to this Function. And Also if i try your Example above i need to return a "Structure", so is it necessary/possible to have a Function which Type is also Structure? Sorry for my Bad English..
I'm French, my English is bad too. Don't worry. I've updated my answer with an example with two function.
Ok nice, Thank you very much! I am not very good in C but i understand now how i can do that.Thanks also to all others which answered, because everyone says the right: "Use Structures not Arrays".
2

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.

1 Comment

Thank you, i will use a structure now. Multiple accepted Answers not possible :/
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.