为什么在scanf()之后该指针变为默认值10?

时间:2016-07-16 20:11:45

标签: c pointers scanf

这个小测试有助于查明我的程序问题。现在发生了一件奇怪的事情:第三个也是最后一个[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 被跳过,紧随其后的测试行为import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class LabelImageText extends JPanel { public LabelImageText() { JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) ); label1.setText( "Easy Way" ); label1.setHorizontalTextPosition(JLabel.CENTER); label1.setVerticalTextPosition(JLabel.CENTER); add( label1 ); // JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) ); label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) ); add( label2 ); JLabel text = new JLabel( "More Control" ); text.setAlignmentX(JLabel.CENTER_ALIGNMENT); label2.add( Box.createVerticalGlue() ); label2.add( text ); label2.add( Box.createVerticalStrut(10) ); // JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) ); label3.setLayout( new GridBagLayout() ); add( label3 ); JLabel text3 = new JLabel(); text3.setText("<html><center>Text<br>over<br>Image<center></html>"); text3.setLocation(20, 20); text3.setSize(text3.getPreferredSize()); label3.add( text3 ); // JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) ); add( label4 ); JTextPane textPane = new JTextPane(); textPane.setText("Add some text that will wrap at your preferred width"); textPane.setEditable( false ); textPane.setOpaque(false); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); StyledDocument doc = textPane.getStyledDocument(); doc.setParagraphAttributes(0, doc.getLength(), center, false); textPane.setBounds(20, 20, 75, 100); label4.add( textPane ); } public static class ColorIcon implements Icon { private Color color; private int width; private int height; public ColorIcon(Color color, int width, int height) { this.color = color; this.width = width; this.height = height; } public int getIconWidth() { return width; } public int getIconHeight() { return height; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(color); g.fillRect(x, y, width, height); } } private static void createAndShowUI() { JFrame frame = new JFrame("LabelImageText"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new LabelImageText() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } 输出值10。

scanf()

最终打印返回cBoard[*tile]为10,无论之前的任务是什么。关于功能不正常void takeTurn(int *iap, int *tile, char *cap) { printf("\n\tPRINT TEST Take Turn()\n"); if (*iap == 1) *cap == 'X'; if (*iap == 2) *cap == 'O'; printf("\nWhich tile would you like?"); printf("\n\tTEST the value of cBoard[0] is %c. At [3] is %c.\n1 ::", cBoard[0], cBoard[3]); scanf("%c", &cBoard[0]); //user inputs 'h'. printf("\n\tTEST the value of cBoard[0] is now %c\n2 ::", cBoard[0]); scanf("%d", tile); //it prints //TEST the value of cBoard[0] is now h //user inputs 6. It prints printf("\n\tTEST the value of cBoard[%d] is %d.\n3 ::", *tile, cBoard[*tile]+1); //it prints //TEST the value of cBoard[6] is 1. scanf("%c", &cBoard[*tile]); //This scanf() does not run. printf("\n\tTEST the value of cBoard[%d] is %d.\n", *tile, cBoard[*tile]); //it prints //TEST the value of cBoard[6] is 10. ... } 的一些事情必须将新值设置为10.

发生了什么事?

1 个答案:

答案 0 :(得分:4)

scanf("%d", tile);读取一个数字,但将'\n'留在标准输入缓冲区中。下一个读取操作scanf("%c", &cBoard[*tile]);读取此字符,该字符恰好具有ASCII中的值10。您可以通过忽略字符前的空格来修复行为:

scanf(" %c", &cBoard[*tile]);  // notice the space before the %c

另请注意,您应检查scanf的返回值,以验证是否提供了正确的输入。