2

I want to understand what this code is supposed to do, 'cause Code::Blocks doesn't show me enough to understand on my own. The code in C language is down bellow:

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
struct S {
   char *S;
};

int main(void) { 
    struct S *S = (struct S *) malloc(sizeof(struct S));
    S -> S = "123\0""45678";
    printf("%d", strlen(S -> S + 5) + S -> S[3]); 
    free(S);
    return 0; 
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
painkiller
  • 149
  • 6
  • Are you sure this is your code? How did you wrote it, if you don't understand? Edit: I'm sry, I overlook a "the" and read "... to understand my code ..." – theSealion Sep 30 '19 at 15:12
  • 1
    @theSealion — I don’t see a claim that it is their own code. – Jonathan Leffler Sep 30 '19 at 15:14
  • 2
    This code is intentionally obfuscated. I suggest you to start with codes which do not try to confuse you but to make it *easy* to the reader to understand what they do. – Eugene Sh. Sep 30 '19 at 15:14
  • It is not my code, I just want to understand it, that's all. Thank you in advance. – painkiller Sep 30 '19 at 15:15
  • 1
    Note that you should not use spaces around the dot `.` or arrow `->` operators; they bind very tightly and spaces are inappropriate and an indication of a neophyte at work. – Jonathan Leffler Sep 30 '19 at 15:20

1 Answers1

2

For starters the code is invalid. In this call

printf("%d", strlen(S -> S + 5) + S -> S[3]); 

there must be used the conversion specifier %zu instead of %d because the expression strlen(S -> S + 5) + S -> S[3] has the type size_t due to the usual arithmetic conversions.

During the preprocessing phase these two strings

"123\0""45678"

are concatenated in one string like

"123\00045678"

where \000 is one character in the octal notation.

So this expression

strlen(S -> S + 5)

yields the length of the substring "5678" that is 4.

This sub-expression

S -> S[3]

yields the character '\000'.

So 4 + 0 gives 4 that is outputted.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Please explain me in detail 2 things : the preprocessing phase where those 2 strings are concatenated into one(wich line of code shows it), and that strlen(S -> S + 5), how did it managed to show only the last 4 digits. Thank you. – painkiller Sep 30 '19 at 15:24
  • 2
    @painkiller In this expression S->S + 5 there is used the pointer arithmetic. So this expression points to the 6th character of the array. It is equivalent to &S->S[5] About the preprocessing phase what is unclear? The compiler concatenates two string with each other.. – Vlad from Moscow Sep 30 '19 at 15:27
  • Ok, so the S->S+5, wich again is &S-S[5], wich is the index 5, but keeping in mind that this is an array, starting from index 0, it is the 6th element. I understand that know. And for the preprocessing phase, what you mean is that the compiler doesn't accept the "123\0""45678" variant, because there are 2 strings, instead of one? so it glues them together into one big string ? without any suplimentary code lines that force him to do that explicitly?(like strcat function does)? Thank you for your answers! – painkiller Sep 30 '19 at 15:34
  • 2
    @painkiller The compiler accepts this record but joins them together as one string. – Vlad from Moscow Sep 30 '19 at 15:37
  • And does it without any further explicit code to do so ? just as part of the compiler internal processing ? – painkiller Sep 30 '19 at 15:39
  • 1
    @painkiller I already said that it is the preprocessing phase of the compilation. What is unclear? – Vlad from Moscow Sep 30 '19 at 15:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200179/discussion-between-painkiller-and-vlad-from-moscow). – painkiller Sep 30 '19 at 15:42