0

Here is a part of my code

int river(int a[], int n){
  int sorted[] = a;
}

n being the size of the array

I have tried a couple of different ways, including

sorted[n]

but I get problems each time. What I am trying to do is duplicate the array with a changed variable name. The array will be exactly the same, just stored in a different variable (because I want to obtain information but not modify the original array).

Is this just not possible because of something to do with dynamically changing the size of the array, or am I just missing the point?

Thanks!

1
  • That's what memcpy is for. " I want to obtain information but not modify the original array" -- wait ... then why do you want to copy it? Just use the original array. Commented Oct 6, 2014 at 7:14

3 Answers 3

4

First of all, arrays are not assignable. You can either copy the contents of one array into another, or get a pointer to the first element of the array. For example

int a[42] = {0};
int* b = a;      // now you can access a's elements as b[0], b[1], etc.
int c[42];
memcpy(c, a, 42*sizeof(int)); //copy elements of a into c

Second, you should note that the function signature

int foo(int a[]);

is another way of saying

int foo(int* a);

i.e. it can take any pointer to int, even ones that don't point to arrays.

With that in mind, you can see that you easily lose array size information, which is why you need to pass the size as a parameter:

int foo(int a[], size_t n);

int a[42];
foo(a, 42);

Now, inside the function, you can copy the elements of one array into another:

int foo(int* a, size_t n)
{
  int sorted[n];
  memcpy(sorted, a, n * sizeof(int));
  // use sorted
  ....
  return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

1+, but nitpicking: int n should be size_t n or even better const size_t n
@alk Agreed. I was just basing it on OP's signature. But I'll change that.
1

You need to allocate space for the new copy of array by yourself.

int *sorted = malloc(n * sizeof(int));
memcpy(sorted, a, n * sizeof(int));

2 Comments

So it is a problem with memory allocation, interesting. Thank you very much!
int sorted[n]; memcpy(sorted, a, n * sizeof *a); -- works too (if you're using a modern C compiler that supports C99 or later).
1

Simply try like this...

   for(int i=0;i<n;i++)
     sorted[i]=a[i];

If you want to use dynamic memory allocation malloc , try this

     int *sorted = malloc(n * sizeof(int));
     memcpy(sorted, a, n * sizeof(int));

2 Comments

Thank you for the answer, but I was trying more so to understand why my implementation wasn't working. Thanks for the help, however!
Whether the array is dynamically allocated or not isn't relevant to whether to use memcpy or a loop ... both work in either case.

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.