site  contact  subhomenews

Can't understand some C code

November 26, 2008 — BarryK
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.

Comments

creates new concatenated s
Username: bob
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.

Free C Books
Username: ChiJoan
"http://www.freebookcentre.net/Language/Free-C-Books-Download.html Hope they help.

That's why there are obfuscated code contests
Username: rarsa
"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

Re: C code
Username: BarryK
"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.

perversity
Username: prehistoric
"Compiler writers commit many such sins. (Mea Culpa.) For those who enjoy this kind of puzzle there is even a book. [url=http://portal.acm.org/citation.cfm?id=SERIES10109.63516]C Puzzle Book[/url] My favorite C reference is the book by Harbison and Steele. [url=http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/013089592X]C: A Reference Manual[/url]


Tags: woof