In article <11**********************@g49g2000cwa.googlegroups .com>,
"nickptar" <ni******@gmail.com> wrote:
Let's say I have a situation like this:
/* begin example.c */
static int* ptr;
static void inner_fn(void)
{
*ptr = 1;
}
void outer_fn(void)
{
int i = 0;
ptr = &i;
inner_fn();
printf("%d\n", i);
}
/* end example.c */
in which a global pointer is set to point to a function-local variable
and then written to in another function. Do I then have to declare `i'
as `volatile' to get the expected behavior?
If the "expected behavior" is that calling inner_fn() sets i to 1, then
there is absolutely no need to declare i as volatile. "volatile" is used
for objects that could be modified by things outside your C code.
Actually, declaring i as volatile int would be a huge mistake: If an
object is volatile, then it is illegal to modify it through a
non-volatile pointer, and it invokes undefined behavior. You would have
to change the declaration of ptr to:
static volatile int* ptr;