Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程:在for循环期间使用struct访问数组中的数组_C_Arrays_Pointers_For Loop_Struct - Fatal编程技术网

C编程:在for循环期间使用struct访问数组中的数组

C编程:在for循环期间使用struct访问数组中的数组,c,arrays,pointers,for-loop,struct,C,Arrays,Pointers,For Loop,Struct,我很难完成这个函数,主要是当我到达for循环并尝试访问数组中的x和y坐标以计算两者之间的距离时 涉及到一个名为locations\u t with fields的结构 x_loc和y_loc以及我的位置数组看起来像 locations[0] = {{0, 0}}; 因此,程序将返回以下输出,但这样做是为了找到位置[i],即距离起始值的最小距离 query_loc = 10, 7 locations[0] = {{10, 10}} //distance between the two is 3

我很难完成这个函数,主要是当我到达for循环并尝试访问数组中的x和y坐标以计算两者之间的距离时

涉及到一个名为locations\u t with fields的结构

x_loc和y_loc以及我的位置数组看起来像

locations[0] = {{0, 0}};
因此,程序将返回以下输出,但这样做是为了找到位置[i],即距离起始值的最小距离

query_loc = 10, 7
locations[0] = {{10, 10}} //distance between the two is 3

query_loc = 10, 7
locations[1] = {{9, 7}} // distance between is 1

// nearest would return locations[1]
这是我的密码

int location_nearest (location_t query_loc, location_t locations[], int num_locations)
{
   //  (Task 5.1) If num_locations equal to or less than 0, return NULL.
    if(num_locations <= 0)
    {
    return NULL;
    }
    //  (Task 5.2) Declare and initialise a pointer to location_t called nearest.
    //  The initial value is the address of the first element in the array.
    location_t *nearest = &locations[0];

    //  (Task 5.3) Declare and initialise an integer called min_dist.
    //  The initial value is the city block distance from the query to
    //  the first element of the array.
    //  Hint: use location_dist.
    int min_dist = location_dist(query_loc, locations[0]);

    //  (Task 5.4) Set up   a for loop to iterate over the array.
    //  Skip the first element of the array, because we already know
    //  the distance from the first element to the query.
    for(int i = 1; i < num_locations; i++)
    {
        //  (Task 5.4.1) Compute the city block distance from the query
        //  to the current element of the array. This is the current
        //  distance. Make sure you remember it somehow.
        int dist = (query_loc.x_loc - locations[i][0]) + (query_loc.y_loc - locations[i][1]);
        //  (Task 5.4.2) If the current distance is less than min_dist:
        if(dist < min_dist)
        {
            //  (Task 5.4.3) Overwrite min_dist with the current distance.
            //  Overwrite nearest with the address of the current element of
            //  the array.
            min_dist = dist;
            nearest = &locations[i]
        }
    }

    //  (Task 5.5) Return nearest.
    return nearest;
}
int-location\u最近(location\u t query\u loc,location\u t locations[],int-num\u locations)
{
//(任务5.1)如果num_位置等于或小于0,则返回NULL。

如果(num_locations如果您这样做
locations[i][0]
您将
locations
变量视为二维数组,它将不会访问结构的第一个成员

为了访问您可以使用的
结构
成员

点(.)
运算符用于非指针变量或
箭头(->)
运算符用于 指针变量后跟成员名称

如下图所示

  int dist = (query_loc.x_loc - locations[i].x_loc) + (query_loc.y_loc - locations[i].y_loc);
而不是

这里有一个问题:

int location_nearest (
^^^
Return type int


location_t *nearest 
^^^^^^^^^^^
nearest is a pointer


return nearest;
       ^^^^^^^
       wrong return type

你能指出哪里出了问题吗?回答不正确,没有编译,分段错误,…?另外,你能通过创建一个函数来完成你的代码吗?为什么你第一次调用
location\u dist
,但在循环内进行实际(一行)计算?int dist=(query\u loc.x\u loc-locations[i][0])+(query_loc.y_loc-locations[i][1]);locations[i][0]产生的表达式必须具有指向对象类型的指针在计算距离时可能要使用
abs(…)
,否则会得到负距离,这些距离将被视为最近的。@Malkeir如果要返回指针:
int location\u nearest(
==>
位置*最近的位置(
int location_nearest (
^^^
Return type int


location_t *nearest 
^^^^^^^^^^^
nearest is a pointer


return nearest;
       ^^^^^^^
       wrong return type