-3

I get this error in C: expression must be a modifiable lvalue

    void bfInsertion(BloomFilter* bloomFilter,const char* elem,int elemLen)
{
    int i = 1;

    while (i <= elem)
    {
        elem[i] = 1;    
        i += 1;
    }

    return elem;
}

I understood that the problem it's about the left side of elem[i] = 1; but I couldn't understand how to do it correctly. Elem is an array of byte and I want to put 1 on the i-th bit.

This is the pseudo-code:

1. i ←1;
2. while i ≤ k do
3. b[hi (delta)] ← 1;
4. i ← i + 1;

PS. I can only modify what's inside the function, not what i pass to it

2 Answers2

0

elem is a pointer to const char. So you can not modify it. Just make it pointer to char only.

  • I can't. I can only modify what's inside the function, not what I pass to it! – Sara Briccoli Nov 08 '20 at 10:42
  • In that case, I must say you are not supposed to change anything in the `elem` variable. I think you have to clarify the logic a bit. Can you explain the pseudo-code you have provided? – Abhijit Mondal Nov 08 '20 at 13:36
0

The elem array is declared const and so cannot be modified. To modify it you would need to remove the const keyword.

You can then use pointer arithmetic to change your value:

*(elem + i) = a value

Though remember that things are numbered from zero in C in general so for the ith value this would be

*(elem + i - 1) = a value
Jon Guiton
  • 1,265
  • 1
  • 9
  • 11