Say I got an array
unsigned char digit[] = {0, 1, 2, 3, 4, 5, 6, 7};
Yet I want to modify part of the array, make the array become something like:
{0, 1, 2, 3, 0, 0, 0, 0}
Enumerate every element I want to modify and alter them might take some effort. Especially when there's a large amount of elements I want to change. I know in some languages like Python I may do something using a single line of code:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
a[4:] = [0, 0, 0, 0]
//a: array([0, 1, 2, 3, 0, 0, 0, 0])
So I wonder, is there a similar way to do that in C?
There are fewer possibilities in C, but in case of an unsigned char
and setting its values to zero you could use memset
:
memset(&digit[4], 0, 4);
Demo.
One options is that you could write a subroutine that would implement the interface that other languages provide "under the cover". You'll probably want to educate yourself on 'VARARGS' to make it take a variable number of arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With