2021年1月27日星期三

A constant being assigned to another constant without errors

I'm currently learning about pointers and the concept of pass-by-value, and I have this C code block:

void doSomething(int b){      b = 6;      printf("%d", b);  }        int a = 5;    int main(void){      doSomething(a);      printf("%d",a);      return 0;  }  

I should get the output 65 with no errors on compilation nor on execution. By tracing the code, here is how I'm seeing it:

  • Integer a is assigned the value 5.
  • Since C is strictly pass-by-value, doSomething(a) == doSomething(5).

Now prior to running the line b = 6;, I'm fairly certain that b == 5. So by running the line, the program is effectively reading:

5 = 6;  

A constant (for a lack of a better term on my part) is being assigned to another constant. In Python this would have failed with a syntax error, and it makes sense to have an error. Why doesn't it raise a syntax error?

https://stackoverflow.com/questions/65929555/a-constant-being-assigned-to-another-constant-without-errors January 28, 2021 at 08:54AM

没有评论:

发表评论