简体   繁体   English

CS50 PSET4指针

[英]CS50 PSET4 pointers

I am attempting CS50 PSET4 . 我正在尝试CS50 PSET4
Can someone explain why the first works instead of the second? 有人可以解释为什么第一个有效而不是第二个有效吗?

Essentially what I did was, I declare char* colour outside the loop in the first and declared char* color inside all of my if statements in the second. 从本质上讲,我在第一个循环外声明了char * color,在第二个循环中声明了所有if语句的char * color。

This worked when I declared char* outside of the if statements 当我在if语句之外声明char *时,此方法有效

 void initBricks(GWindow window)
 {
    char* colour;
    // TODO
    for(int i=0,y=20;i < ROWS; i++)
    { 
        y+= 30;

      for(int j=0,x=5,c=0;j < COLS; j++)
      {
        if(i==0)
        colour = "RED";

        if(i==1)
        colour = "BLUE";

        if(i==2)
        colour = "CYAN";

        if(i==3)
        colour ="ORANGE";

        if(i==4)
        colour = "GRAY";

        GRect brick = newGRect(x,y,30,15);
        setFilled(brick,true);
        setColor(brick, colour);
        add(window, brick);       
        x+= 40;       


      }
   }   
}

But this didn't work, when I declared char* inside all the if statements 但是,当我在所有if语句中声明char *时,此方法不起作用

void initBricks(GWindow window)
{
    // TODO
    for(int i=0,y=20;i < ROWS; i++)
    { 
        y+= 30;

      for(int j=0,x=5,c=0;j < COLS; j++)
      {
        if(i==0)
        char *colour = "RED";

        if(i==1)
        char *colour = "BLUE";

        if(i==2)
        char *colour = "CYAN";

        if(i==3)
        char *colour ="ORANGE";

        if(i==4)
        char *colour = "GRAY";

        GRect brick = newGRect(x,y,30,15);
        setFilled(brick,true);
        setColor(brick, colour);
        add(window, brick);       
        x+= 40;       

     }
   }   
}

I am fairly new to pointers but so far I sort of understand that char* is sort of the equivalent of a string where it points to the address of the variable, colour, in this case. 我对指针还很陌生,但是到目前为止,我有点理解char *在某种程度上相当于一个字符串,在这种情况下,char *指向变量的地址colour。

However, I am not sure why I don't have to put in the '&' (reference operator) when I use it in setColor(brick, colour) . 但是,我不确定为什么在setColor(brick, colour)使用它时不必添加'&' (引用运算符setColor(brick, colour)

To see why the second group of code doesn't work it may be helpful to see it as: 要了解为什么第二组代码不起作用,将其视为:

    if (i==0) {
        char *colour = "RED";
    }

    if (i==1) {
        char *colour = "BLUE";
    }

You can see more easily that the declaration of colour extends only to the end of the block, so that colour no longer exists when the next statement is executed. 您可以更容易地看到colour的声明仅延伸到块的末尾,因此在执行下一条语句时不再存在colour

As for your second question, setColor is only using the value of colour , (which is already a pointer) so no need to pass a reference to it. 至于第二个问题, setColor仅使用colour的值(已经是一个指针),因此无需传递对其的引用。 setcolor can access the string without the reference. setcolor可以访问没有引用的字符串。

The reason why the second set doesn't work is that you are attempting to modify a string literal (char *colour = "foo") which is not allowed in C. 第二组不起作用的原因是您试图修改C中不允许的字符串文字(char * colour =“ foo”)。

As an aside, both versions have undefined behavior, and you should consider compiling with all of the -W flags on. 顺便说一句,两个版本都有未定义的行为,您应该考虑使用所有-W标志进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM