-2

I have a unsigned const volatile short int*. I want it to be (x + y), which at time of defining is set to 0. However, i want if for some reason y changes to 5, i would want the unsigned const volatile short int* to change too.

Would this be possible in C?

(Note: I am using freestanding C99 mode with GNU extensions, and i mean for it to change automatically and not with a function.)

180Five
  • 13
  • 5
  • Variables in C do not update their values reptrospectively: there is no dependency system (unlike a spreadsheet). Also, remove the `const` if you don't actually want it to be `const`. And doesn't `const` contradict `volatile`? – Weather Vane Feb 23 '17 at 19:31
  • The pointer is not `const` qualified, so yes, it is of course allowed to be changed. – too honest for this site Feb 23 '17 at 19:58
  • @WeatherVane no, const doesn't contradict volatile. [They're orthogonal in C](http://stackoverflow.com/questions/4592762/difference-between-const-const-volatile). – Antti Haapala -- Слава Україні Feb 23 '17 at 20:12
  • @Olaf did you mean that it's the pointer that is `const` qualified, and not what it points to? OP is not changing any pointer, but what it points to. – Weather Vane Feb 23 '17 at 20:18
  • @WeatherVane: I just read the text; there is no [mcve]. "I have a `unsigned const volatile short int*`" - which is a pointer. "I want it to be `(x + y)`" - no idea what `x` and `y` are, but he asks clearly to set the pointer with their sum. – too honest for this site Feb 23 '17 at 20:44
  • @Olaf yes unclear question. – Weather Vane Feb 23 '17 at 20:47
  • Your questions is unclear. It looks like you want the pointer to change. And why is the object it points to `volatile` qualified? You seem to have some missconceptions about the C language. A good book might help. – too honest for this site Feb 23 '17 at 20:47

2 Answers2

1

There is no way to make variables automatically update based on other variables like that in C.

Instead of storing the sum of x and y in a variable, you should consider just making a function that recomputes the sum whenever you need it. The addition should be pretty fast:

int get_the_sum()
{
    return x + y;
}

Alternatively, you might consider making x and y be static variables that can only be changed with setter functions. The setter functions for x and y would take care of updating any variables that need to be updated. It's hard to tell whether this approach is worth it without knowing more about your application.

void change_y(int new_y)
{
  y = new_y;
  sum = x + y;
}
David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

Supposing that y is declared as

volatile unsigned short int y = -x;

(or an equivalent), and that the value of y is initially different from 5, you can define

unsigned const volatile short int *p = &y;

This will satisfy the requirements as you presented them, though it imposes conditions you did not specify. In addition to those explicitly given above, it assumes that the fact that you require changes in response to changes in y, but do not mention changes in response to changes in x means that the pointed-to value in fact does not need to respond to changes in x.

Of course, I'm inclined to think that this doesn't at all represent what you really mean. That cannot be implemented in C. Pointers refer to locations in memory, but the value of an expression such as x + y does not reside in memory.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157