0

I am trying to create a 2d Array at compile time that has an unknown number of rows that i can dynamically allocate throughout the program but a specific number of columns as 8.

Something like ---->Elements[?][8];

theForgottenCoder
  • 145
  • 1
  • 1
  • 11
  • Thanks for all the solutions I will try and implement a few of them tomorrow and see which works best for my application, and choose the best answer. Thank you ! – theForgottenCoder Mar 13 '14 at 06:33

5 Answers5

3

If you have to use 2d array instead of list of array you gonna have to make a array

constant i = 1
foo[i][8]

and every time you want to expand that array

make temp_foo[i][8]
copy foo to temp_foo
delete foo
make foo[i++][8]
copy temp_foo to foo

But that's make confusing. and i think its better if use link list

struct node
{
 foo[8]
 node *next;
}

adding first element

node *element_head
element->foo = {add elements}
element->next = null

adding new element

node *temp
temp->foo = {add element}
temp->next = element_head
element_head= temp
mhs
  • 1,012
  • 2
  • 14
  • 35
2

Knowing the number of columns, and making only the number of rows dynamic you can either use a VLA or dynamic allocation. A VLA is straight forward:

int rows;

// get rows somehow

int table[rows][8];

Keeping in mind a VLA has automatic storage lifetime and will be removed from addressable memory once the enclosing scope expires. And they cannot be globals.

If your implementation doesn't support VLA's, automatic storage space is a concern, or you need a global variable for some nefarious purpose, you'll have to manage this dynamically (which it sounds like you want to do anyway). To do that, declare a pointer to an array of 8 elements, as such:

int rows;

// get rows somehow

int (*table)[8] = malloc(rows * sizeof(*table));

The rest is straight forward. You can reference your elements as table[i][j] for i in 0..rows-1 and j in 0..7. Just remember to free your allocation when finished:

free(table);

and don't reference it again.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
1

I'd go with this approach when the dimensions are unknown.
Assuming data type to be int.

int* a; //this will point to your 2D array

allocate it when you know the dimensions (ROW, COL):

a = malloc(sizeof(int)*ROW*COL);

and access it like

a[ROW*i + j] = value  // equivalent of a[i][j]
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
1

As far as I know, you can't have foo[][8] in C. You might be able to hack around it by making a struct and casting a pointer to that struct to an array, as discussed here, but that is a somewhat fragile hack.

What you can do is change the definition of rows and columns in your problem space, so that, in order to access row i, column j, you would do foo[j][i] instead of foo[i][j]. In this case you could declare your array like this: <typename> * foo[8].

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • You *can* have it, though the syntax isn't as you show here. It is `type (*foo)[8];` Further, said-variable is valid to pass as a function parameter declared as: `void func(type ar[][8])`. – WhozCraig Mar 13 '14 at 05:54
  • Well, i did say "as far as I know". I'm glad I learned something by posting this answer. :) – merlin2011 Mar 13 '14 at 07:19
0

I think it will not be created when you are not passing any value at compile time, my suggestion is to use dynamic memory allocation as you don't know how many rows

Yaswanth
  • 89
  • 7