1

I'm exercising a bit in programming C. One task is to concat two dynamic arrays. The elements of the second array should be added to the end of the first array. The following is given:

void concatArrays(int* numbers1, int length1, int* numbers2, int length2)
{
  //code
}

And that is my code to solve the task:

#include <stdio.h>
#include <stdlib.h>

void concatArrays(int* numbers1, int length1, int* numbers2, int length2)
{
  numbers1 = (int*)realloc(numbers1, sizeof(int*) * (length1 + length2));
  for (int count = 0; count < length2; count++)
  {
    numbers1[length1 + count] = numbers2[count];
  }
}

int main()
{
  int* num = (int*)malloc(sizeof(int*) * 6);
  num[0] = 1;
  num[1] = 2;
  num[2] = 3;
  num[3] = 4;
  num[4] = 5;
  num[5] = 6;
  int* numbers = (int*)malloc(sizeof(int*) * 4);
  numbers[0] = 1;
  numbers[1] = 2;
  numbers[2] = 3;
  numbers[3] = 4;
  concatArrays(num, 6, numbers, 4);
  for (int count = 0; count < 10; count++)
  {
    printf("%d - ", num[count]);
  }
  return 0;
}

Unfortunately, it doesn't work. I do know that the code does work if I used a pointer to a pointer:

void concatArrays(int** numbers1, int length1, int** numbers2, int length2) { //code }

Nonetheless, that seems to be not allowed regarding the task requirements.

Do you have any idea how I could change my code meeting the requirements to solve the task?

Thank you in advance.

Edit:

I forgot:

The output: 1 - 2 - 3 - 4 - 5 - 6 - 2054454589 - 32767 - -1280384664 - 32767 -

trincot
  • 317,000
  • 35
  • 244
  • 286
AnUser1
  • 79
  • 8

2 Answers2

1
void concatArrays(int* numbers1, int length1, int* numbers2, int length2)

Given prototype is pass by value for your case.

Hence when you reallocate the memory.

  numbers1 = (int*)realloc(numbers1, sizeof(int*) * (length1 + length2));

You are allocating for local copy not for original copy.

Note that it is not guaranteed that new pointer returned by realloc will be same as old.

code does work if I used a pointer to a pointer.

That is because you will be passing by reference any modification inside function will update the original variables.


Thus allocate the more memory in the main itself.

 int* num = (int*)malloc(sizeof(int) * 10);
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Thank you for your answer! It already helped me. Nevertheless, a main-function is not necessary according to the task. I just made one for testing purpose to see whether my code works or not. Hence, I can't do that in the main itself and I can't assume the number of elements (in my personal example 10) as given. Do you have another idea? Would help me a lot! – AnUser1 Dec 01 '18 at 16:29
  • 1
    @AnUser1 There are no other ways other than pass by reference. – kiran Biradar Dec 01 '18 at 17:58
  • 2
    @AnUser1: While the signature of the function remains unchanged, you can't return a useful result. You can either return the pointer to the newly allocated memory (leaving the two arrays untouched, I'd suggest, though then you should use `const int *numbers1` in the signature), or take an `int **result` argument and use that. If you need to return the length as well as the data, then you start thinking about an 'array of `int`' structure type, along the lines of the one I just created in an answer to [How to extract all the numbers from a string](https://stackoverflow.com/questions/53572030/). – Jonathan Leffler Dec 01 '18 at 18:53
  • @JonathanLeffler and kiranBiradar : Thank you very much! – AnUser1 Dec 02 '18 at 14:45
0

There is a failure in the memory allocation, respective on type.

Note that the values of array are ints, so by the allocation they occupy not a pointer size memory more likely int size spaces:

so the allocation should look like this for int arrays:

void concatArrays(int** numbers1, int* length1, const int* numbers2, const int length2)
{
    *length1 = *length1 + length2;
    *numbers1 = (int*)realloc(*numbers1, sizeof(int) * (*length1));
    ...
}

Note that the size of an int pointer (int*) may be different than the size of a pure int type.

Aak
  • 182
  • 9
  • 1
    Or maybe a memory padding is occured to the array. The array's memory size probably isn't equal to `n * sizeof(int *)` – Taha Paksu Dec 01 '18 at 15:12
  • Oh, I have forgot that the first array will be modified, also the location of it, then the first parameter need to be added as a reference: void concatArrays(int*& numbers1, int length1, int* numbers2, int length2) – Aak Dec 01 '18 at 15:44
  • thanks @Osiris, repaired with simulated pass-by-reference parameters – Aak Dec 02 '18 at 21:22