C
Chad
Can someone tell my why:
char i;
int a;
Causes unconstrained behavior.
Thanks in advance.
Chad
char i;
int a;
Causes unconstrained behavior.
Thanks in advance.
Chad
Can someone tell my why:
char i;
int a;
Causes unconstrained behavior.
Chad said:Can someone tell my why:
char i;
int a;
Causes unconstrained behavior.
#include <stdio.h>
void Func1()
{
printf("func1\n");
return;
}
void Func2()
{
printf("func2\n");
return;
}
void Func3()
{
printf("func3\n");
return;
}
void StartFunctions( void (*f)(void) )
{
printf("Starting Function...\n");
f();
return;
}
int main()
{
char i;
void *v[] = { (void*)Func1, (void*)Func2, (void*)Func3 };
for ( i = 0; i < 3; i++ ) StartFunctions( v );
return 0;
Chad said:Can someone tell my why:
char i;
int a;
Causes unconstrained behavior.
Chad said:I misworded it. The question stems from the following thread:
http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0
One of the responses was: [snip]{
char i;
Using a character value for array subscripting is at least suspicious;
no, wait, it's actually a constraint violation. Use:
int i; [...]
The question I meant to ask was why does 'char i' cause a constraint
violation, but 'int i' wouldnt?
Keith said:Chad said:I misworded it. The question stems from the following thread:
http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0
One of the responses was:
[snip]
{
char i;
Using a character value for array subscripting is at least suspicious;
no, wait, it's actually a constraint violation. Use:
int i;
[...]
The question I meant to ask was why does 'char i' cause a constraint
violation, but 'int i' wouldnt?
Ok, that's completely different from what you posted. In your
original post, you were using i as the dimension in an array
declaration; here, you're just using it as an array index.
Using a char value as an array index does not violate a constraint.
The constraints on an array subscripting expression are (with
underscores denoting italics):
One of the expressions shall have type "pointer to object _type_",
the other expression shall have integer type, and the result has
type "_type_".
Since char is an integer type, it's perfectly legal to use a char as
an array index.
It is suspicious, though. It's implementation-defined whether char is
signed or unsigned, and the value is promoted to int (or conceivably
to unsigned int) anyway. The space saving for a single declared
object is insignificant. There's almost never a good reason to use a
char as an array index.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.