0

I am going to have an array of arrays in c as each array in the main array has a specific size. For example: { {a,b,c}, {a}, {a,d} } The problem is I don't want to use heap memory and to use "malloc", also it should not be any wasted memory. For example I don't want to use following code:

char myArrays[][3] = {
 {a,b,c},
 {a,null,null},
 {a,d,null}
} 
RezKesh
  • 2,861
  • 2
  • 24
  • 35

3 Answers3

6

If you want to make an array of arrays of different sizes, you need to make an array of pointers, like this:

char *myArrays[] = {
    (char[]){'a','b','c'},
    (char[]){'a'},
    (char[]){'a','d'}
};

You do not need to "pad" your arrays with null characters.

Note that this approach does not provide an easy way of finding out the exact length of inner arrays: sizeof operator is not going to work. If you want to know the lengths of the inner arrays, either add terminating entries of some sort (say, '\0's) or add an array of lengths, like this:

size_t myLengths[] = {3, 1, 2};

Now you can iterate the array of arrays like this:

for (int i = 0 ; i != 3 ; i++) {
    for (int j = 0 ; j != myLengths[i] ; j++) {
        putchar(myArrays[i][j]);
    }
    putchar('\n');
}

Demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Does that really work? Neat. – Peter - Reinstate Monica Jun 25 '14 at 11:47
  • @PeterSchneider Absolutely, it does! I added a demo. – Sergey Kalinichenko Jun 25 '14 at 11:49
  • 3
    To clarify for those who've been living under a rock the past 15 years: this solution uses the C concept of _compound literals_ which was introduced with the C99 standard. (It's not really necessary to use them though, you might as well declare each array as an individual variable and achieve the very same result.) – Lundin Jun 25 '14 at 12:55
  • Thanks @dasblinkenlight , but let suppose that I have defined a _struct_ in my code. Does your answer works to make a array of arrays of pointers to my instances of the _struct_ ? – RezKesh Jun 26 '14 at 18:53
  • 1
    @rezCash Yes, you could use this concept with expressions defined in code ([demo](http://ideone.com/0DaEBb)). Of course you need to pay attention to the scope of that array and the `struct`s that go into it in order to avoid undefined behavior. – Sergey Kalinichenko Jun 26 '14 at 18:59
0

Declare an array of pointers:

char* ptrArray [] =
{
  array1,
  array2,
  ...
};

To keep track of their individual sizes, either declare another array with the same size containing the sizes, or embedded the arrays in structs, where the size is one member and the pointer to the array another.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • It may you mean that I should declare each array and then use them in _ptrArray_ , but I want just initiate each array in the main array. – RezKesh Jun 26 '14 at 18:45
  • @rezCash For what purpose? Do you have any rational reason for wanting to have them in the same array, or do you just "feel" that way? – Lundin Jun 27 '14 at 06:11
-1

Now in C++ you could make a vector of vectors. Something like the following.

std::vector< std::vector<char> > myArrays;
myArrays.resize(3);
myArrays[0].resize(3);
myArrays[1].resize(1);
myArrays[2].resize(2);

But this is C++ and not C. In addition, std::vector uses heap memory behind the scenes.

But this is the closest to what you have asked for that I can think of.

Benilda Key
  • 2,836
  • 1
  • 22
  • 34
  • 1
    The question is about C, so your answer is off-topic and irrelevant. Also the OP specifically asked for a solution that doesn't use heap memory. – Lundin Jun 25 '14 at 12:52