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};

6
  • 2
    You cannot change sizes of arrays. You'll have to pad the rest with zeros or something. Commented Dec 7, 2019 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. Commented Dec 7, 2019 at 18:56
  • 1
    memcpy(a, b, sizeof(b)); memset(a + (sizeof(b) / sizeof(b[0])), 0, sizeof(a) - sizeof(b)); Commented Dec 7, 2019 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. Commented Dec 7, 2019 at 20:02
  • A loss of array size is less of a problem than an increase! ;-) Commented Dec 7, 2019 at 21:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

That you can assign pointers to each other has zero to do with how the storage they point to was created ;-)
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
You can assign pointers to arrays no matter where the arrays are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.