M
Michael Schumacher
Hello everybody,
this question came up recently in a local C NG, and it's about
how to correctly apply the "const" qualifier, when the object
(i.e., a variable) is "double-referenced" (pointer to pointer
to object).
Example: say, we have a character array,
char mystring[100];
and a pointer to some character in that array:
char *ptr = &mystring[42];
Now we'd like to define a function, say "myparse()", that
takes "&ptr" as a parameter, so that it can advance "*ptr"
to the end of the current token. However, we do *not* want
"myparse()" to modify the characters (i.e., the compiler should
emit an error message if things like "**ptr = 'x';" occurr),
*and* we don't want the compiler to emit a warning when
calling "myparse (&ptr);" due to incompatible pointer types.
I will intentionally _not_ show you all the variants I already
tried, in order to not confuse or "bias" you. To summarize:
void
myparse ("type of pointer to pointer to constant chars" x)
{
++(*x); // should be allowed
++(**x); // should produce an error message
}
void
foo (void)
{
char mystring[100];
char *ptr = &mystring[42];
myparse (&ptr); // should work w/out any cast or warning!
}
Any ideas how to accomplish this in ISO-C (FWIW, I'm using gcc
4.3.2 and the options "-c -Wall -std=c99 -pedantic")?
Thanks,
mike
this question came up recently in a local C NG, and it's about
how to correctly apply the "const" qualifier, when the object
(i.e., a variable) is "double-referenced" (pointer to pointer
to object).
Example: say, we have a character array,
char mystring[100];
and a pointer to some character in that array:
char *ptr = &mystring[42];
Now we'd like to define a function, say "myparse()", that
takes "&ptr" as a parameter, so that it can advance "*ptr"
to the end of the current token. However, we do *not* want
"myparse()" to modify the characters (i.e., the compiler should
emit an error message if things like "**ptr = 'x';" occurr),
*and* we don't want the compiler to emit a warning when
calling "myparse (&ptr);" due to incompatible pointer types.
I will intentionally _not_ show you all the variants I already
tried, in order to not confuse or "bias" you. To summarize:
void
myparse ("type of pointer to pointer to constant chars" x)
{
++(*x); // should be allowed
++(**x); // should produce an error message
}
void
foo (void)
{
char mystring[100];
char *ptr = &mystring[42];
myparse (&ptr); // should work w/out any cast or warning!
}
Any ideas how to accomplish this in ISO-C (FWIW, I'm using gcc
4.3.2 and the options "-c -Wall -std=c99 -pedantic")?
Thanks,
mike