The following program gives different results when compiled using gcc compiler, and turbo C
#include<stdio.h>
#include<string.h>
void main()
{
char* c = "gatecs2017";
char* p = c;
printf( "%d", (int)strlen( c + 2[p] - 6[p] - 1 ) );
}
Somebody please explain the working of the program. Also why it generates different results?
David Ranieri :
strlen(c+2[p]-6[p]-1) is translated to strlen(((c + 't') - '2') - 1) = strlen(((c + 116) - 50) - 1), thus, accessing outside of the bounds of the string (undefined behaviour).",
2017-02-14T08:57:28