I read the standard but still cannot be sure:
#include <stdio.h>
#include <string.h>
void repl(char *restrict ap){
char *cp=strchr(ap,(int)'m');
*cp='M';
}
int main(){
char arr[] = "example";
repl(arr);
puts(arr);
return 0;
}
In the function repl
, I used strchr
to get another pointer for modifying the object. I expect the result is the string with the first m
replaced with M
.
But could this be undefined behavior?
If yes, then what about using ap[cp-ap]='M';
instead of *cp='M';
?