为什么我的函数中的for循环只会运行一次,即使它应该运行多次?

xtr*_*rap 0 c++ arrays for-loop class c++11

有人可以向我解释为什么这个for循环只运行一次,无论n是什么:

double CalcDist(unsigned int n, Point p, Point* s)
{
    double sd[n];
    for(int i = 0; i < n; i++)
    {
        sd[i] = s[i].Dist_To(p);
        return sd[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

Bat*_*eba 8

return过早退出函数,并在for循环体内.

另外,混音时要非常小心,unsignedsigned在使用表达式时键入类型i < n.你知道如果n是0 会发生什么?