S
somenath
Hi All,
The following program is crashing.
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char s[] ="test";
char *t=s;
while(*t)
{
*t = toupper(*t++);
//t++;
}
printf("\n%s\n",s);
printf("\n");
return 0;
}
But according to me it may not have undefined behaviour. So let me
describe my understanding line by line.
char s[] ="test";
This line define a modifiable character array and initialize it to
"test";
char *t=s;
s is a pointer the first element of the character array "test" and has
type as char *. That value is assigned to t. Which is legal.
while(*t)
If the value of *t is '\0' , the expression while (*t) will be false
and the loop will end.
*t = toupper(*t++);
The = operator has associativity as right to left so it is evaluates
from right to left so toupper(*t++); will be evaluated first the the
value will be assigned to *t. ++ has higher precedence than *
operator so t++ will be evaluated then the value of *t++ will be
passed to the function toupper and as a side effect of t++ , t will
be pointing to the next character in the s array.
the return value of toupper will be place in the *t.
This will continue until *t become '\0'.
If I do not increment 't' in this expression *t = toupper(*t); but
do it in next line it work as expected.
Where my understanding going wrong?
I think the crash may be due to the following reason.
====================
In the expression *t = toupper(*t++); *t (the left hand side of = )
may have the updated value that is the result of t++.
So if t++ point to '\0' and dereference that can cause undefined
behaviour.
But how that can happen also ? As post increment operator's side
effect will be taking effect after the sequence point in this case
after execution of the code *t = toupper(*t++);
I am not sure.
I find the C standard very cryptic to get answer. Is there any book
which describe the standard with example also can be understood by non
experts? Is there any easy way to know different causes of undefined
behaviour in an expression?
Regards,
Somenath
The following program is crashing.
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char s[] ="test";
char *t=s;
while(*t)
{
*t = toupper(*t++);
//t++;
}
printf("\n%s\n",s);
printf("\n");
return 0;
}
But according to me it may not have undefined behaviour. So let me
describe my understanding line by line.
char s[] ="test";
This line define a modifiable character array and initialize it to
"test";
char *t=s;
s is a pointer the first element of the character array "test" and has
type as char *. That value is assigned to t. Which is legal.
while(*t)
If the value of *t is '\0' , the expression while (*t) will be false
and the loop will end.
*t = toupper(*t++);
The = operator has associativity as right to left so it is evaluates
from right to left so toupper(*t++); will be evaluated first the the
value will be assigned to *t. ++ has higher precedence than *
operator so t++ will be evaluated then the value of *t++ will be
passed to the function toupper and as a side effect of t++ , t will
be pointing to the next character in the s array.
the return value of toupper will be place in the *t.
This will continue until *t become '\0'.
If I do not increment 't' in this expression *t = toupper(*t); but
do it in next line it work as expected.
Where my understanding going wrong?
I think the crash may be due to the following reason.
====================
In the expression *t = toupper(*t++); *t (the left hand side of = )
may have the updated value that is the result of t++.
So if t++ point to '\0' and dereference that can cause undefined
behaviour.
But how that can happen also ? As post increment operator's side
effect will be taking effect after the sequence point in this case
after execution of the code *t = toupper(*t++);
I am not sure.
I find the C standard very cryptic to get answer. Is there any book
which describe the standard with example also can be understood by non
experts? Is there any easy way to know different causes of undefined
behaviour in an expression?
Regards,
Somenath