簡體   English   中英

包含結構數組的結構不能由相同類型列表中的值分配

[英]Struct containing an array of struct cannot be allocated by values from a list of the same type

我的問題如下:我有一個名為Environment的結構,其中包含Obstacle結構的列表。 我無法從代碼中手動創建的現有Obstacle列表中分配此environment.obstacles

我有以下定義:

struct PointGPS {
    double longitude;
    double latitude;
} typedef PointGPS;

struct Obstacle {
    int id;
    PointGPS position;
    double radius;
} typedef Obstacle;

struct Environment {
    Obstacle *obstacles;
    PointGPS destination;
} typedef Environment;

我正在使用以下函數實例化我的值:

PointGPS createPoint(double longitude, double latitude) {
    PointGPS point;
    point.latitude = latitude;
    point.longitude = longitude;
    return point;
}

Obstacle createObstacle(int id, double radius, PointGPS position) {
    Obstacle obstacle;
    obstacle.id = id;
    obstacle.radius = radius;
    obstacle.position = position;
    return obstacle;
}

Environment createEnvironment(PointGPS destination, Obstacle *obstacles) {
    Environment environment;
    environment.destination = destination;
    environment.obstacles = obstacles;
    return environment;
}

main功能中,我正在創建障礙列表:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int main() {
    PointGPS pointGps1 = createPoint(49.202538, 6.918930);
    PointGPS pointGps2 = createPoint(49.202650, 6.925839);
    PointGPS destination = createPoint(49.202660, 6.925849);
    Obstacle obstacle1 = createObstacle(1, 10, pointGps1);
    Obstacle obstacle2 = createObstacle(2, 30, pointGps2);
    Obstacle *obstacles[2];
    obstacles[0] = &obstacle1;
    obstacles[1] = &obstacle2;
    // environment is global
    environment = createEnvironment(destination, *obstacles);
    // The two following methods allow me to print the id of the first element
    printf("%d\n", environment.obstacles->id);
    printf("%d\n", environment.obstacles[0].id);
    // But I cant access to the values -for exemple the id field- of the following obstacles
    printf("%d\n", environment.obstacles[1].id);
}

我想將此清單提供給我的environment 但是它無法分配或指向正確的值。 根據environment.obstacles中某些障礙物的id進行測試,障礙物總是從內存中返回一些隨機整數值。

我嘗試了以下嘗試:

1)

// environment is global
environment = createEnvironment(destination, *obstacles);

2)

environment.obstacles = (Obstacle *)malloc(sizeof(Obstacle *) * NELEMS(obstacles));
environment.obstacles[0] = *obstacles[0];
environment.obstacles[1] = *obstacles[1];

3)

environment.obstacles = (Obstacle *)malloc(sizeof(Obstacle *) * NELEMS(obstacles));
environment.obstacles[0] = createObstacle(obstacles[0]->id, obstacles[0]->radius, createPoint(obstacles[0]->position.longitude, obstacles[0]->position.latitude));
environment.obstacles[1] = createObstacle(obstacles[1]->id, obstacles[1]->radius, 
createPoint(obstacles[1]->position.longitude, obstacles[1]->position.latitude));

所有這些嘗試都失敗了,返回了-2144113376值,甚至沒有在我的environment變量中創建length = 2的數組。

我很確定自己缺少明顯的東西,因為我對這種語言的經驗為零,但我無法弄清楚。 我該如何進行?

更新1

這種語法使我可以使用方法n°1進入第一個障礙:

printf("%d\n", environment.obstacles->id)

但是我無法進入第二個障礙。 進行printf("%d\\n", environment.obstacles[1]->id); 返回此錯誤:

Member reference type 'Obstacle' (aka 'struct Obstacle') is not a pointer, did you mean to use '.'?

並執行printf("%d\\n", environment.obstacles[1].id); 仍然返回隨機值。

更新2

根據要求,我編輯了結構,功能和主體。

更新3

這是一個可測試的代碼,可以完成我在帖子中嘗試的操作。 https://onlinegdb.com/S1342WPsN如果我在計算機上運行代碼,則在嘗試訪問第一個字段之后的字段時,仍然會有隨機值。 但是,如果我在網站內運行代碼(請參閱鏈接),似乎可以提供正確的價值。

數組和指針之間存在混淆。 obstacles在你的main功能是指針數組Obstacle的結構。 您可能想要定義一個Obstacle結構數組,並使用obstacle1obstacle2副本進行初始化,然后將其傳遞給createEnvironment 將數組作為參數傳遞會有效地將指針傳遞到其第一個元素。

這是修改后的版本:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int main() {
    PointGPS pointGps1 = createPoint(49.202538, 6.918930);
    PointGPS pointGps2 = createPoint(49.202650, 6.925839);
    PointGPS destination = createPoint(49.202660, 6.925849);
    Obstacle obstacle1 = createObstacle(1, 10, pointGps1);
    Obstacle obstacle2 = createObstacle(2, 30, pointGps2);
    Obstacle obstacles[2];
    obstacles[0] = obstacle1;
    obstacles[1] = obstacle2;
    // environment is global
    environment = createEnvironment(destination, obstacles);
    // The two following methods allow me to print the id of the first element
    printf("%d\n", environment.obstacles->id);
    printf("%d\n", environment.obstacles[0].id);
    // This one should print the id of the second element
    printf("%d\n", environment.obstacles[1].id);
    return 0;
}

還需要注意的是你的tyedef定義不是C.慣用的typedef關鍵字通常是第一位的:

typedef struct PointGPS {
    double longitude;
    double latitude;
} PointGPS;

只是為問題添加圖片,這就是您的代碼中定義的(原諒我的UML。)

數據類型。

您所做的就是創建的。

數據。

具體來說, *obstaclesobstacles[0] = &obstacle1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM