4

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?

1
  • 1
    There is a way to do it in one line of code but I would not recommend doing it this way: memset(&digit[4], 0, sizeof(digit) - (4 * sizeof(digit[0]))); Commented Jan 8, 2018 at 3:16

4 Answers 4

3

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.

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

4 Comments

In this case it solves my question, thank you! But since you said possibilities, I wonder if there's some other ways? Like what if I want to change digit to {0, 1, 2, 3, 0, 9, 17, 0}, that is, alter it with several different value.
@AmarthGûl C99 standard does not allow this, but gcc has a nice extension that could be used with memcpy. Here is a demo.
@dasblinkenlight why wouldn't C99 allow that?
@AnttiHaapala Oops, I thought this was an extension, but it's actually part of C99. Thanks for your comment!
3

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.

Comments

3

Others have already mentioned setting the array elements to a single value using memset, as a part of your follow up question you asked if some elements can be set to certain values like {1, 2, 3, 4}.

You can use memcpy here to achieve that. Since your type here is unsigned char I will keep that, but in general this method can be used for any type.

memcpy(&digit[4], ((unsigned char[4]){1, 2, 3, 4}), 4 * sizeof(unsigned char));

You can see the demo here.

2 Comments

The sizeof(unsigned char) is unnecessary since a) sizeof returns the size in chars and, b) memcpy copies the given number of chars (bytes).
@AnttiHaapala I agree with the comment that it is unnecessary but I wanted to make the statement general across types. I have also mentioned in the answer that this method can be used for any type. I had a feeling that OP wouldn't know how to extend it to other types (not trying to be disrespectful but careful).
0

I think that maybe not the shortest but something you can easy do is just:

digit[] = {0, 1, 2, 3, 4, 5, 6, 7};   %Having this

a=the number in your vector you want to start making ceros;
n=the lenght of digit;
for(i=a;i=n;i++)
{
    digit[n]=0;
}

Is just a way I think you could use. If you want to change an specific one just

b=position;
digit[b]=c; %Where c is the number you want to put in there.

I hope it works for you, good luck.

3 Comments

I think I did, maybe I understood something wrong, can I ask you why are you asking?
If it's because of the variable n that you can know with the function sizeof(), I think it's because of that. But correct me if it's not that.
This answer describes the method the OP is specifically trying to avoid.

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.