1

I plan to create an 2D array of a pointer derived from typedef struct

Let's say the typedef struct is named "Items" and contains mixed variables of strings and integers.

I will declare two intvariables namely typenum and typetotal. These two integers will start off from Zero and adds up when the input data matches with certain function.

In the array,Items *type[][], basically type[][] is Items *type[typenum][typetotal] but I cannot do this since I will declare typenum and typetotal as zero at the declaration part.

I tried initializing the array through Items *type[][] = {{0},{0}} but this generates error.

Any advice? Some told me to use malloc() on this, but I simply do not know how.

*Using Tiny C on Windows

Beginner C
  • 79
  • 3
  • 12
  • Don't listen to the answers posted here but instead check out [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Apr 17 '18 at 14:08

2 Answers2

3

Use dynamic memory allocation.

Items **type;
type = malloc(sizeof (Items *) * typenum);

for (int i = 0; i < typenum; i++)
    type[i] = malloc(sizeof Items) * typetotal);

You need to manually free the allocated memory after using the array.

for (int i = 0; i < typenum; i++)
    free(types[i]);

free(types);

Here is a tutorial on it: http://www.eskimo.com/~scs/cclass/int/sx9b.html

Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Oct 01 '13 at 09:23
  • @Rafi Kamal Will this code work if I want to initialize single bracket array? – Beginner C Oct 01 '13 at 09:23
  • @BeginnerC You mean 1D array? It will work with some modification. Have a look at this tutorial: http://www.cplusplus.com/reference/cstdlib/malloc/ – Rafi Kamal Oct 01 '13 at 09:27
  • @RafiKamal Can't I put the `free(types[i];` after `type[i] = malloc(sizeof Items) * typetotal);`? – Beginner C Oct 01 '13 at 09:30
  • `malloc` is used for allocating memory to a pointer. `free` is for freeing the allocated memory when your program no longer needs it. You will call `free` only after you are done with the array. Please check the two tutorials I've given, you'll find detailed information. – Rafi Kamal Oct 01 '13 at 09:33
0

If typenum and typetotal increase as your program runs be sure to use realloc, which will reallocate more memory and keep the contents. You'll need to allocate the first dimension of the array like this:

myArray = realloc(myArray, sizeof(Items*) * typenum);

and then allocate the second dimension for each of the first:

for(...)
    myArray[i] = realloc(myArray[i], sizeof(Items) * typetotal);
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • Will I use `realloc` everytime when the `typenum` and `typetotal` increases? – Beginner C Oct 01 '13 at 09:29
  • That's right. Beware of running out of memory, in which it'll return NULL but leave the *old* memory allocated, not freed, likely memory leak!! – noelicus Oct 01 '13 at 09:32
  • Just so that it is clear to me, I shall put `type[i] = malloc(sizeof Items) * typetotal)` first then below it, I will put your suggested code, `type[i] = realloc(type[i],sizeof(Items) * typetotal);` Then after that, I will add `free(type[i]);` then I will end the bracket for the `for(...)` and put `free(types);`? – Beginner C Oct 01 '13 at 09:35
  • no, use realloc instead. When you pass NULL into the realloc call it'll act the same as malloc. – noelicus Oct 01 '13 at 09:36