1

I am wondering if it is possible in C to overwrite the contents of one array with another, and by doing so, how do I work around with the loss of array size?

Say I have two arrays.

int a[] = {1,2,3,4,6,7,8};
int b[] = {1,6};

// How can I overwrite the array a with array b, so get the following?:
a[] = {1,6};

Grey_dev1997
  • 77
  • 1
  • 6
  • 2
    You cannot change sizes of arrays. You'll have to pad the rest with zeros or something. – klutt Dec 07 '19 at 18:37
  • dynamic arrays can be shrinked and expanded. Create dynamic arrays and then copy one to another by decreasing/increasing the size of one. – bUff23 Dec 07 '19 at 18:56
  • 1
    `memcpy(a, b, sizeof(b)); memset(a + (sizeof(b) / sizeof(b[0])), 0, sizeof(a) - sizeof(b));` – David Ranieri Dec 07 '19 at 19:06
  • Since you cannot change the size of an array at run-time. you have to do something else. It is not possible to advise because that "something" will depend on what you are trying to achieve. So ask a question about a concrete example where you need to do this. For example you might have a sentinel value to indicate the end of useful data, or an additional variable containing the length of useful data. – Clifford Dec 07 '19 at 20:02
  • A loss of array size is less of a problem than an increase! ;-) – Peter - Reinstate Monica Dec 07 '19 at 21:19
  • And what you can always do is put an array in a struct; you *can* assign structs of equal type. (The [reasons you can assign structs but not arrays](https://stackoverflow.com/q/33291624/3150802) are part historic and part pragmatic.) – Peter - Reinstate Monica Dec 07 '19 at 21:20

2 Answers2

1

When you declare an array like a[] = {...} the array gets a fixed size which cannot be altered, the size is determined when you compile.

If you want to use dynamic arrays you need to allocate on the heap, in C this is done with malloc and realloc. realloc allows you to resize an array.

e.g.

char* p = malloc(10);
char* q = realloc(p, 5); // now you made the array 5 bytes shorter

you should check the return value of realloc in order to know whether the realloc was successful.

char* q = realloc(p, 5);
if (q != NULL) // successful

ref: https://en.cppreference.com/w/c/memory/realloc

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

You cannot do that, like @Klutt & @david-ranieri said in comments you can copy the content of b in a and then fill with 0.
But I recommend to use dynamic array, since C++11 you can do :

// initialise with braced-init-list
int* a = new double[8] { 1, 2, 3, 4, 5, 6, 7, 8};
int* b = new double[2] { 1, 6};

delete[] a;
a = b;
Ben Souchet
  • 1,450
  • 6
  • 20
  • That you can assign pointers to each other has zero to do with how the storage they point to was created ;-) – Peter - Reinstate Monica Dec 07 '19 at 21:17
  • I disagree, If you create an array (stack) of size `8 * sizeof(int)` you cannot transform it to an array of size `2 * sizeof(int)`, but if you use pointers (heap) you only point to the beginning of the allocated area so you can tell the memory that now `a` point to the beginning of the area pointed by `b` – Ben Souchet Dec 07 '19 at 21:24
  • You can assign pointers to arrays no matter where the arrays are. – Peter - Reinstate Monica Dec 07 '19 at 21:32