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?
-
Why not pass the array, starting and end index of the subarray you want as argument to that function. – asad_hussain Jul 24 '16 at 04:34
-
This question might help you . http://stackoverflow.com/questions/19646512/passing-an-array-by-reference-in-c – rUCHit31 Jul 24 '16 at 04:35
-
Maybe this might help: https://stackoverflow.com/questions/14618342/copying-a-subset-of-an-array-into-another-array-array-slicing-in-c – jpw Jul 24 '16 at 04: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. – Jonathan Leffler Jul 24 '16 at 04:50
-
1You 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. – Keith Thompson Jul 24 '16 at 04:59
4 Answers
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 *slice_array(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!

- 59,041
- 12
- 98
- 151
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

- 50
- 1
- 7
#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;
}

- 85
- 1
- 6
-
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)` – Keith Thompson Jul 24 '16 at 04:57
-
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? – bluesawdust Jul 24 '16 at 05:02
-
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). – Keith Thompson Jul 24 '16 at 05:06
-
2I hadn't tried calling my functions incorrectly, heh. Updated the answer appropriately. – bluesawdust Jul 24 '16 at 05:16
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);

- 58
- 3