Home:ALL Converter>Different output for the string function strlen() in C

Different output for the string function strlen() in C

Ask Time:2017-02-14T16:51:18         Author:Sharon

Json Formatter

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?

Author:Sharon,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42221564/different-output-for-the-string-function-strlen-in-c
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
yy