Can't understand some C code

I think that some of you guys who read this blog have a good knowledge of C. I wonder if someone could help me to understand one of the lines in the following code.

Here is a Genie program:
init

s:string = "My name is Barry"
s = s + " Kauler"
print(s)


The Vala compiler has the '-C' switch which outputs C code. Here is the essential part of it:

	char* s;

char* _tmp0;
char* _tmp1;
s = g_strdup ("My name is Barry");
_tmp0 = NULL;
s = (_tmp0 = g_strconcat (s, " Kauler", NULL), (s = (g_free (s), NULL)), _tmp0);
_tmp1 = NULL;
g_print ((_tmp1 = g_strconcat (s, "\n", NULL)));
_tmp1 = (g_free (_tmp1), NULL);
s = (g_free (s), NULL);


I only have one C reference book, "C for yourself" published by Microsoft, 1988. I can't find anywhere in that book that describes the syntax of this line:

s = (_tmp0 = g_strconcat (s, " Kauler", NULL), (s = (g_free (s), NULL)), _tmp0);

...does anyone understand that line?

Note, those functions starting with 'g_' are described here:
http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html
They are really just wrappers for the libc functions, strdup() etc.

I'm starting to write some tutorials on programming with Genie, but early days yet. Eventually they'll go up on the puppylinux.com site, well probably as I substantially complete each page I'll upload it.


Posted on 26 Nov 2008, 20:48


Comments:

Posted on 26 Nov 2008, 22:20 by bob
creates new concatenated s
It adds "Kauler" to the end of the string s but creates new storage as a side effect, points _tmp0 at the new storage, frees the old storage at s, then assigns the value of _tmp0 to s. Comma-separated expressions are evaluated left to right, and the value of the last expression get assigns.



Posted on 26 Nov 2008, 24:58 by ChiJoan
Free C Books
http://www.freebookcentre.net/Language/Free-C-Books-Download.html

Hope they help.


Posted on 27 Nov 2008, 2:16 by rarsa
That's why there are obfuscated code contests
This can be read as several lines of code:

_tmp0 = g_strconcat (s, " Kauler", NULL);
g_free (s);
s = NULL;
s = _tmp0;

I don't know if there are advantages for writing it in a single line other than winning and obfuscated code contest


Posted on 27 Nov 2008, 5:16 by otto
Another reason ...
... could be that this is machine generated code, not intended for human consumption. One gets scared at witnessing the assembler code generated by some of the smartest compilers.


Posted on 27 Nov 2008, 8:04 by BarryK
Re: C code
Ah, so the brackets ( .... ) mark a series of C instructions, and the commas are delimiters, meaning execute everything in this code block, left to right. I didn't know about the comma operator, and looking in my C book, yes it is there.

Thanks guys, for clearing that up!

Yes, normally it is not intended to see that C code. The Vala compiler generates C code then calls the Gnu C compiler, so we are not concerned with the intermediate C code. However, the Vala compiler can be told to output the C code, and the Gnu C compiler can be run separately.


Posted on 29 Nov 2008, 8:50 by prehistoric
perversity
Compiler writers commit many such sins. (Mea Culpa.) For those who enjoy this kind of puzzle there is even a book. C Puzzle Book

My favorite C reference is the book by Harbison and Steele. C: A Reference Manual