Author: Richard Pijl
Date: 14:48:00 05/27/04
Go up one level in this thread
>>>>> >>>>>1)I asked the computer to print the moves in the book only to discover that >>>>>printf("%s %s",move1,move2) does not work. >>>> >>>>Are move1 and move2 char* 's? If so, that should be fine. >>>>What's happening? >>>> >>>> >>>>-- >>>>James >>> >>>I do not know but for me it did not work and the computer printed the first >>>string twice instead of printing 2 different strings. >>> >>>Try the following code that is part of movei: >>> >>> >>>#define rankfrom(u) (((u)>>3)&7) >>>#define rankto(u) (((u)>>9)&7) >>>#define filefrom(u) ((u)&7) >>>#define fileto(u) (((u)>>6)&7) >>>#define promotion(u) ((u)&(1<<29)) >>>#define promote(u) (((u)>>16)&255) >>> >>>char *move_str(int move) >>>{ >>> static char str[6]; >>> >>> char c; >>> >>> if (promotion(move)) { >>> switch (promote(move)) { >>> case KNIGHT: >>> c = 'n'; >>> break; >>> case BISHOP: >>> c = 'b'; >>> break; >>> case ROOK: >>> c = 'r'; >>> break; >>> default: >>> c = 'q'; >>> break; >>> } >>> sprintf(str, "%c%d%c%d%c", >>> filefrom(move) + 'a', >>> rankfrom(move)+1, >>> fileto(move) + 'a', >>> rankto(move)+1, >>> c); >>> } >>> else >>> { >>> sprintf(str, "%c%d%c%d", >>> filefrom(move) + 'a', >>> rankfrom(move)+1, >>> fileto(move) + 'a', >>> rankto(move)+1); >>> } >>> return str; >>>} >>> >>>printf("%s %s",move_str(1),move_str(0)); >>> >>>//result b1a1 b1a1 >>> >>>printf("%s %s",move_str(0),move_str(1)); >>> >>>//result a1a1 a1a1 >>> >>>Uri >> >>This is an easy one: your strings share one common place in memory, namely the >>variable static char str[6]. Therefore when you call move_str() for the second >>time, the first string gets overwrited. >>Filip > >I do not understand why they share one place in memory. > >The computer is supposed to do the following: > >1)calculate move_str(0) >2)forget every local varaible including str[6] This one is a local 'static' variable. It keeps its value. >3)calculate move_str(1) This will overwrite str. >4)forget every local varaible. > remember that an array variable is equivalent to a pointer in C. You're returning a pointer to the array str, not the string itself. return str; is equivalent with return &str[0]; >Note that it seems to do it in that way when I do it in 2 different printf for 2 >strings but not when I use one printf. First both functions are called, and then the string is built by printf, so you will get the same move twice. Richard.
This page took 0.02 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.