5

I have an array int arr[5] = {10, 2, 3, 5, 1}, and I want to pass in the last 4 elements (basically from index 1 to index4) into an argument as an array (so: [2, 3, 5, 1]). Is there a way to do this very simply (like how in Ruby you would be able to do arr[1..4]), or would I have to use a for loop?

5
  • Why not pass the array, starting and end index of the subarray you want as argument to that function. Commented Jul 24, 2016 at 4:34
  • This question might help you . stackoverflow.com/questions/19646512/… Commented Jul 24, 2016 at 4:35
  • Maybe this might help: stackoverflow.com/questions/14618342/… Commented Jul 24, 2016 at 4:40
  • &arr[1] does the job — as long as you don't want to pass a copy (the function isn't going to modify the array, or it doesn't matter if it does). If you want to copy, you have to do the copying manually. Commented Jul 24, 2016 at 4:50
  • 2
    You can't pass arrays as function arguments in C. Instead, you can pass the address of the initial element and the size as separate arguments. Commented Jul 24, 2016 at 4:59

4 Answers 4

5

You can manually increment the pointer by 1:

your_function(arr + 1)

Pointer arithmetic in C implicitly accounts for the size of the elements, so adding 1 will actually add 1 * sizeof(int)

For a closer analogue to array slicing from other languages, try this function:

int *copy_array_slice(int *array, int start, int end) {
    int numElements = (end - start + 1);
    int numBytes = sizeof(int) * numElements;
    
    int *slice = malloc(numBytes);
    memcpy(slice, array + start, numBytes);
    
    return slice;
}

It makes a slice of the array between the given start and end indices. Remember to free() the slice once you're done with it!

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

Comments

3

Answer

Given you current code:

int arr[5] = {10, 2, 3, 5, 1};

You can duplicate the range 1..4 by:

int arr_dup[4];
memcpy(arr_dup,arr+1,sizeof(int)*4);

Remember that your function definition should be a pointer, example:

void a_function(int *arr_arg); //Call via a_function(arr_dup);

Explanation

Arrays in c implemented as pointers (aka variables that hold memory addresses).

If you do arithmetic on the pointer, it will advance to the respective element. Example:

ptr + 1 //Next Element
ptr - 1 //Previous Element

Comments

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

void my_function(int arr_size, int *arr)
{
  int i;
  for(i=0; i < arr_size; i++)
    {
      printf("[%d]:%d\n", i, arr[i]);
    }
}

int main(int argc, char **argv)
{
  int arr[] = { 10, 2, 3, 5, 1 };
  (void)my_function(4, &arr[1]); /* TODO > use more flexible indexing */
  return EXIT_SUCCESS;
}

4 Comments

Why are you using old-style pre-ANSI function definitions? Use prototypes; void my_function(int arr_size, int *arr), int main(int argc, char **argv)
Explain why there is an appreciable difference. No compiler nor standard was mentioned, so I picked a historical style. I work in older code bases and have come to like this style of function definition due to the ease of documenting parameters. Additionally, why would I use prototypes for something so simple?
Old-style function declarations and definitions have been obsolescent since 1989. Prototypes were added to the language because old-style declarations do not let the compiler check the correctness of calls. You could write my_function("foo", 12.3, 42, NULL) and the compiler wouldn't complain (but it would probably blow up at run time).
I hadn't tried calling my functions incorrectly, heh. Updated the answer appropriately.
2

I think you can use memcpy,memcpy can copy data byte to byte.In memmory,our data is binary,so even int is 4 bytes,we can copy it byte to byte.


int dst[4];
memcpy(dst,&arr[1],size(int) * 4);

Comments

Your Answer

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