当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Bugs in a code snippet

问题描述:

Spot all bugs in the code snippet below

uint arr[100]

for (uint i=99; i >=0; i--)

arr[i] = 0;

This is a question for the test, can anybody point me to all bugs in this snippet

网友答案:
  1. uint isn't a type.
  2. The first line is missing a semicolon.
  3. i >= 0 is always true.
  4. arr[0U - 1] is undefined behavior because it access outside the bounds of the arr array.
  5. It's not clear that this snippet is running as part of a function. If it is not, then the entire for-loop is a syntax error.

Additionally, if this class is taking place before 1999, then:

  1. You can't declare variables in a for loop. Instead, the uint i should be declared before the loop.

This code should probably be rewritten as simply:

unsigned arr[100] = {0};
分享给朋友:
您可能感兴趣的文章:
随机阅读: