On Mon, 8 Apr 1996, The Gathering MUD wrote: > Hello.. i have a question.. Hello.. I have an answer. :-) > sprintf(buf2, "%s[ Exits: %s]%s\r\n", CCCYN(ch, C_NRM), > +-> *buf ? buf : "None! ", CCNRM(ch, C_NRM)); > | send_to_char(buf2, ch); > | > Would someone please explain to me what this line does? It is from > act.informative.c, in the autoexit code, around line 300 In pseudoenglish: if the first character in buf is not '\0', then use buf. otherwise, use the string "None! " I assume you can figure out the rest of that line yourself. The ?: operator works like an if..else block, but 'returns' the results. The format is: (expr) ? (if expr non-zero) : (if expr zero) That probably wasn't clear...here's some examples: All the numbers are arbitrary, they can be anything. int a = 1, b = 2, c; c = (a > 0) ? 1 : 0; // c will be set to 1 c = (a < 0) ? 1 : 0; // c will be set to 0 c = (a > b) ? a : b; // c will be set to b (2) because a is not greater // than b Anything you can assign from something can be used in the "if expr (non)zero" parts. This includes the result of a function, a pointer, variable, etc. The above three examples converted to if..else: if (a > 0) c = 1; else c = 0; if (a < 0) c = 1; else c = 0; if (a > b) c = a; else c = b; The advantage of ?: is that it's an operator, returning a result, rather than a statement. It can be used anywhere a value can be used (like the sprintf() in the original code). Hope I explained that well enough. If not, time to get a good book on C programming. Of course, you should have one anyway if you're coding. - Mark markd@eskimo.com (finger markd@eskimo.com for my PGP signature)
This archive was generated by hypermail 2b30 : 12/18/00 PST