Subj : Re: STRTOK Question To : borland.public.cpp.borlandcpp From : waking@idirect.com (Wayne A. King) Date : Wed Sep 03 2003 06:13 pm On Wed, 03 Sep 2003 12:00:40 -0400, Steven wrote: > char input[16] = "a,b,c,d"; > printf("%s %s %s %s\n", strtok(NULL , ","), strtok(NULL, ","), >strtok(NULL, ","), strtok(input, ",")); > printf("%s %s %s %s\n", strtok(input, ","), strtok(NULL, ","), >strtok(NULL, ","), strtok(NULL , ",")); >Why does the second line of output produce 3 (null) values? Two primary facts to recognize: (1) The order of evaluation of the arguments to printf (and to function calls in general) is not specified by the language Standard. It may evaluate right to left or left to right. There is no guarantee that the first (or last) argument will be evaluated before the others. The order may vary from compiler to compiler. Never rely on the expectation of a specific order. (2) The buffer is directly modified by strtok. Thus, even if you executed the first printf twice the results would be different each time. The first pass would replace all of the commas in "input" with the nul character. e.g. - int i; for(i = 0; i<2; ++i) { printf("%s %s %s %s\n", strtok(NULL , ","), strtok(NULL, ","), strtok(NULL, ","), strtok(input, ",")); } would give the following *with your implementation*: d c b a (null) (null) (null) a (Results may vary with other compilers due to a different order of printf arg evaluation.) -- Wayne A. King (ba994@torfree.net, wayne.king@ablelink.org, waking@idirect.com, Wayne_A_King@compuserve.com) .