-3

Is it possible to create an array of arrays in c

Thank you.

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • Related: http://stackoverflow.com/questions/455960/dynamic-allocating-array-of-arrays-in-c – jonsca Aug 29 '11 at 11:52
  • 1
    While this question isn't quite a dupe, http://stackoverflow.com/questions/6899800/what-is-the-output-please-explain-considering-i-am-a-novice-in-c/6901705#6901705 is a very good answer to most of your questions. – Wooble Aug 29 '11 at 11:53
  • See faq on [How do I use array](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – cpx Aug 29 '11 at 11:57
  • I thought you needed an array of arrays where each array can have a subarray! – nikosdi Aug 29 '11 at 12:11
  • well i need one array were i can have arrays of different sizes within it. Seems using a multidimensional array will work fine without having to write unnecessary code. Thanks Guys – pnizzle Aug 30 '11 at 15:05

4 Answers4

1

It's the same as for example in PHP:

int arrayInArray[10][50];

You read data out of it with:

printf("%d", arrayInArray[3][37]);
Organized
  • 352
  • 3
  • 9
  • hahaha this I know, but you have removed the blinkers from my eyes, this will work quite fine for what I what, simple and sraight. Thanks ! – pnizzle Aug 30 '11 at 15:03
1

I bet you mean Multi Dimensional Array instead of "array of arrays".

Some links for this topic:

petermolnar
  • 1,626
  • 14
  • 23
1

For using an array of arrays with all the power of C you should have some knowledge of dynamic memory handling in c, with the functions malloc, realloc, and free, and some knowledge about pointers. For this example that you ask a possible solution would be this:

#include <stdio.h>
void main(int argc, char* argv[]){

    int** myArray; /* This would be a double pointer, because you want a two dimension array.*/
    int firstDimension = 10;
    int secondDimension = 20;
    int i;

    myArray = (int**)malloc(firstDimension*sizeof(int*)); This way you initialize the first dimension of the array.

    for(i = 0; i < firstDimension; i++){
        myArray[i] = (int*)malloc(secondDimension*sizeof(int));
    }

    /*Once you have the array initialized, you can access in the way myArray[i][j];*/

   /*For releasing resources */
    for(i = 0; i < firstDimension; i++){
        free(myArray[i]);
    }
    free(myArray);
}

This is the dynamic way, the one that is teached on CS courses.

Roberto Luis Bisbé
  • 2,080
  • 1
  • 15
  • 22
1

If you need an array of arrays then you should use structs.

typedef ArrayStruct* ArrayStructPtr;

struct ArrayStruct
    {
void* array;//Node array
ArrayStructPtr arrays;//Pointer to sub arrays
};
int main()
{
ArrayStruct* a;//Declare Some Arrays
a=(ArrayStruct*)malloc(sizeof(ArrayStruct)*N);
for(int i=0;i<N;i++)
{
a[i].array=(void*)malloc(sizeof(int)*N);//Malloc the actual array
a[i].arrays=NULL;//Malloc subarrays if needed
}
//add subarray on array 0
ArrayStruck * temp=(ArrayStruct*)malloc(sizeof(ArrayStruct));
temp->array=(void*)malloc(sizeof(char)*MAXNAME*N);
temp->arrays=NULL;
a[0]=arrays=temp;
return 0;
}

What you need is a List Of arrays Where each node of the struct can hold an array and a pointer to another node. The array type is void* to support int,float,char*.

So each array can have as many subarrays as you want.You can create 3 dimension Arrays if you want!

nikosdi
  • 2,138
  • 5
  • 26
  • 35