-1

I have a struct which contains an array of ints. I do not want to fix the size of the array as I don't know how many elements will go into it and do not want to waste memory.

An array of int pointers would be the same essentially.

I tried a pointer to an array of integers and a double pointer to no avail.

How could I accomplish this?

NOTE:

What I mean by array of int pointers is that if I set a fixed size array of int pointers then I am still wasting space, so in that sense it makes no difference with a fixed size array of ints. The only difference is that an int and int pointer may be different in size.

Mars
  • 4,677
  • 8
  • 43
  • 65

3 Answers3

4

An array of int pointers would be the same essentially.

No, you mean a pointer (int *):

int *arr = malloc(sizeof(*arr) * n);

If you don't know n beforehand:

int n = 0, *arr = NULL;

while (condition) {
    arr = realloc(arr, sizeof(*arr) * (n + 1));
    n++;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • I don't know `n` beforehand though. – Mars Oct 15 '14 at 05:40
  • I want to grow the array like a `vector`. – Mars Oct 15 '14 at 05:40
  • @Zaphod: That's what dynamic memory is all about. If you want that sort of data structure than you can A) implement it, or B) find an existing implementation. Nothing in standard C will do this for you. – Ed S. Oct 15 '14 at 05:41
  • But you do will know 'n' (number of array elements) runtime right ?? – vvvv Oct 15 '14 at 05:41
  • 1
    @Zaphod Are you asking for a linked list? – Peter R Oct 15 '14 at 05:44
  • @Zaphod: It is not possible to have an array and at the same time not to know the number of elements. It is self-contradictory. The number of elements can change at run-time, but an any given moment it must be a specific number. – AnT stands with Russia Oct 15 '14 at 05:45
  • @AndreyT. Sure, but that's not the point. The point is I do not want to fix the number initially. – Mars Oct 15 '14 at 05:49
  • 2
    @Zaphod: You don't have to *fix* it neither initially nor at any other moment. This means that at any run-time moment you can increase it or decrease it using `realloc`. That's the only way "not to fix" the array size in C. Yet at any moment (including "initially") the array will have to have a specific size. Which means that you will have to *choose* some initial size in any case. Later you will be able to change it if it proves insufficient (or excessive). – AnT stands with Russia Oct 15 '14 at 06:02
  • @AndreyT. I ended up doing slightly different but yes, `realloc` did the trick. – Mars Oct 15 '14 at 06:07
1

An array of int pointers would be the same essentially

Not really. An array of pointers has absolutely nothing to do with it.

What you need is an int *a pointer, which you will use as a pointer to the first element of an array of integers. That array of integers you will allocate yourself at run-time thorough malloc with appropriate size. You will be able to grow your array, if the need arises, using realloc.

If your struct type has only one run-time-sized array in it, you have another option known as a "struct hack". There is plenty of information on "struct hack" that you can search for. Here's one example: C struct hack at work

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

You can use this trick:

struct stuff
{
  int arr[1];
};

Then when you create one of these, you use malloc(sizeof(struct stuff) + n*sizeof(int)) where n is how many extra integers you want to store. Then you can access them as usual, e.g. mystuff.arr[100] so long as n was at least 100.

In C99 you can instead say int arr[]; and prior to C99 if you have GCC you can do int arr[0];. This of course makes it easier to create empty arrays if you want that.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436