-3

Is it possible to create an array of arrays in c

Thank you.

5
  • Related: stackoverflow.com/questions/455960/… Commented Aug 29, 2011 at 11:52
  • 1
    While this question isn't quite a dupe, stackoverflow.com/questions/6899800/… is a very good answer to most of your questions. Commented Aug 29, 2011 at 11:53
  • See faq on How do I use array Commented Aug 29, 2011 at 11:57
  • I thought you needed an array of arrays where each array can have a subarray! Commented Aug 29, 2011 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 Commented Aug 30, 2011 at 15:05

4 Answers 4

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

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 !
1

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

Some links for this topic:

Comments

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.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.