while语句带函数

时间:2015-10-22 18:00:58

标签: c while-loop

我有一个功能

int is_true(int a[]){
    int is_true;

    if(a[0] != a[5]){
        is_true= 0;
    }else if(a[2] != a[4]){
        is_true= 0;
    }else{
        is_true= 1;
    }
    return is_true;
}

我有另一个函数,我想插入一个while循环,条件是只要is_true = 0它就会继续。但我一直收到这个警告:

  

函数'is_true'的比较总是等于空指针   false [-Wetutological-pointer-compare] pal.c / exercise1 line 61 C / C ++   问题

我的其他功能是

while(is_true == 0){
        c = getchar();

    if(c =='a'){
        if(p ==&a[0]){
            printf("Sorry, no more moves this way \n");
        }else{
        p--;
        printf("pointer is now at: %d\n ", *p);
        }
    }
    else if( c=='d'){
        p++;
        printf("pointer is now at: %d\n ", *p);
    }
    else if(c=='w'){
        (*p) = (*p+1);
        printf("the value is now: %d\n ", (*p));
    }
    else if(c=='s'){
                (*p) = (*p-1);
                printf("the value is now: %d\n ", (*p));
    }
}

3 个答案:

答案 0 :(得分:4)

您有一个函数和一个名称相同的变量 if(n == 0) { return "*"; } else { return starStr(n - 1) + "**"; } 。不要那样做。

答案 1 :(得分:1)

你的函数import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.util.Locale; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingUtilities; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import de.javagl.viewer.Painter; import de.javagl.viewer.Viewer; public class StringBoundsUtilsTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10); private static Font font = DEFAULT_FONT.deriveFont(10f); private static void createAndShowGUI() { JFrame f = new JFrame("Viewer"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new BorderLayout()); Viewer viewer = new Viewer(); String string = "AbcXyz"; viewer.addPainter(new Painter() { @Override public void paint(Graphics2D g, AffineTransform worldToScreen, double w, double h) { AffineTransform at = g.getTransform(); g.setColor(Color.BLACK); g.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Rectangle2D boundsA = StringBoundsUtilsPerformance.computeStringBoundsDefault( string, font); Rectangle2D boundsB = StringBoundsUtils.computeStringBounds(string, font); g.setFont(new Font("Monospaced", Font.BOLD, 12)); g.setColor(Color.GREEN); g.drawString(createString(boundsA), 10, 20); g.setColor(Color.RED); g.drawString(createString(boundsB), 10, 40); g.setFont(font); g.transform(worldToScreen); g.drawString(string, 0, 0); g.setTransform(at); g.setColor(Color.GREEN); g.draw(worldToScreen.createTransformedShape(boundsA)); g.setColor(Color.RED); g.draw(worldToScreen.createTransformedShape(boundsB)); } }); f.getContentPane().add(viewer, BorderLayout.CENTER); f.getContentPane().add( new JLabel("Mouse wheel: Zoom, " + "Right mouse drags: Move, " + "Left mouse drags: Rotate"), BorderLayout.NORTH); JSpinner fontSizeSpinner = new JSpinner(new SpinnerNumberModel(10.0, 0.1, 100.0, 0.1)); fontSizeSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { Object object = fontSizeSpinner.getValue(); Number number = (Number)object; float fontSize = number.floatValue(); font = DEFAULT_FONT.deriveFont(fontSize); viewer.repaint(); } }); JPanel p = new JPanel(); p.add(new JLabel("Font size"), BorderLayout.WEST); p.add(fontSizeSpinner, BorderLayout.CENTER); f.getContentPane().add(p, BorderLayout.SOUTH); viewer.setPreferredSize(new Dimension(1000,500)); viewer.setDisplayedWorldArea(-15,-15,30,30); f.pack(); viewer.setPreferredSize(null); f.setLocationRelativeTo(null); f.setVisible(true); } private static String createString(Rectangle2D r) { return String.format(Locale.ENGLISH, "x=%12.4f y=%12.4f w=%12.4f h=%12.4f", r.getX(), r.getY(), r.getWidth(), r.getHeight()); } } 有一个同名的局部变量。要解决这个问题,只需将函数名更改为is_true即可。

答案 2 :(得分:1)

假设您的while循环可见的标识符is_true引用您提供的函数is_true(),错误指出您正在将指向该函数的指针与空指针进行比较常数,哪种比较总是评估为假。

你想要做的当然是将0与调用函数的结果进行比较,如下所示:

while (is_true(a) == 0) {
    /* ... */
}

,假设所需的参数为a

请注意,函数的局部变量ais_true与程序中任何其他位置的同名变量之间没有固有的关系。