【问题标题】:invalid type argument of '->' editing fields of a structure from a separate function'->' 从单独的函数编辑结构的字段的无效类型参数
【发布时间】:2020-09-23 10:19:54
【问题描述】:

我在从单独的函数编辑结构字段时遇到问题,我正在尝试从更新无人机信息函数编辑我的无人机结构的字段。基本上我对所有箭头都得到相同的错误(无效的类型参数'->')

我确定这个问题源于我对指针缺乏了解

任何帮助将不胜感激:)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10

typedef struct{
//define struct info and variables
    int drone_number;
    char drone_name[20];
    int year_manufactured;
    double mass;
    double top_speed;
    double max_distance;
    double load_capacity;

} drone_info;

int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {

    int searchID, numdrones, i, drYrMan;    
    double drMass, drTopSpeed, drMaxDist, drLoadCap;
    char drname[20];
    numdrones = no_of_drones;

    printf("what drone ID would you like to update?: ");
    scanf("%d", &searchID);
    printf("name: ");
    scanf("%s", drname);
    printf("year manufactured: ");
    scanf("%d", &drYrMan);
    printf("mass: ");
    scanf("%lf", &drMass);
    printf("top speed: ");
    scanf("%lf", &drTopSpeed);
    printf("max distance: ");
    scanf("%lf", &drMaxDist);
    printf("load capacity: ");
    scanf("%lf", &drLoadCap);

    droneinfo[searchID]->drone_number = searchID;
    droneinfo[searchID]->drone_name = drname;
    droneinfo[searchID]->year_manufactured = drYrMan;
    droneinfo[searchID]->mass = drMass;
    droneinfo[searchID]->top_speed = drTopSpeed;
    droneinfo[searchID]->max_distance = drMaxDist;
    droneinfo[searchID]->load_capacity = drLoadCap;



    for(i=0; i < numdrones; i++){




    }

return 0;

}



//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){


int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;


scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
    if (!strcmp(namechoice, droneinfo[i].drone_name)) {

printf("found a match\n\nID: %d Name: %s  Year: %d  Mass: %.2f  Top Speed: %.2f  Max Distance: %.2f Load Capacity: %.2f\n", 
        droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);


    found = 1;
    }

}

if(found == 0){
    printf("\nNo matches were found!\n");
}

return 0;

//make condition for all

}

int main(void)  {

drone_info droneinfo[10];   

int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;

//open the drone.txt file where the drone info is stored
    FILE* inputfile = fopen("drone.txt", "r");
    if(inputfile == NULL)
    {
        perror("ERROR! ");
        exit(-1);
    }

//initialise the function that puts the struct in an array
    while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
        if(ferror(inputfile)){
            perror("An error occurred: ");

        }

        droneinfo[i].drone_number = droneID;
        strcpy(droneinfo[i].drone_name, dronename);
        droneinfo[i].year_manufactured = yrman;
        droneinfo[i].mass = dronemass;
        droneinfo[i].top_speed = dronetopspd;
        droneinfo[i].max_distance = dronemaxdist;
        droneinfo[i].load_capacity = droneload;

        i++;
    }

    no_of_drones = i;

    fclose(inputfile);

    //print the dtone info in an array
    printf("Data:\n\n");
    for (i=0; i < no_of_drones; i++){
        printf("ID: %d Name: %s  Year: %d  Mass: %.2f  Top Speed: %.2f  Max Distance: %.2f Load Capacity: %.2f\n", 
        droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);

    }


do{
//program menu with appropriate menu items
  printf("Please select an option:\n\n");
  printf("1. Input/update drone information\n");
  printf("2. Search a drone\n");
  printf("3. Simulate a drone delivery scenario\n");
  printf("4. Display simulation results\n");
  printf("5. Save drone information\n");
  printf("6. Save all results\n");
  printf("7. Exit\n\n");

  scanf("%d", &choice);
    //switch for the 7 available menu cases
    switch(choice)
    {
    case 1:
    //input drone function
    updateDroneInfo(droneinfo, no_of_drones);

    break;

    case 2:
    //search drone function
    searchDroneName(droneinfo, no_of_drones);

    break;



    case 3:
    //simulate drone function


    break;


    case 4:
    //display simulation results


    break;


    case 5:
    //save drone information


    break;



    case 6:
    //save all results function


    break;



    case 7:
    //exit/breaks the loop

    break;


    default:
      printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
    break;


    }
}   while(choice != 7);
return 0;

}   

重新

【问题讨论】:

  • droneinfo 是一个drone_info 结构对象的数组,它不是一个指针数组。因此,您需要像使用任何其他非指针结构一样使用.
  • droneinfo[searchID]-&gt;drone_number -> droneinfo[searchID].drone_number。诚然,编译器的错误信息不是很好。
  • 欢迎来到 SO。对于未来的问题,请尝试将您的代码减少到重现问题所需的最低限度。你展示了大量不相关的代码。对于编译错误,通常只需要非常有限的代码。
  • 你也可以直接读入结构成员,如scanf("%d", &amp;droneinfo[searchID].drone_number)
  • 顺便说一句(并且与您的问题完全无关),但这是一些在线挑战或作业,还是只是您当地班级的作业?我想知道因为this question 正在解决完全相同的问题。如果是本地课堂作业,那么也许你们是同学,可以互相帮助?

标签: c function pointers struct structure


【解决方案1】:

在这样的陈述中:

droneinfo[searchID]->drone_number = searchID;

表达式droneinfo[searchID] 不是指针。它的类型为drone_info,因为指针droneinfo 已被下标运算符取消引用。

你必须写:

droneinfo[searchID].drone_number = searchID;

数组也没有赋值运算符。您需要将元素元素从一个数组复制到另一个数组。

代替这个语句:

droneinfo[searchID]->drone_name = drname;

你必须使用标准的字符串函数strcpy

$include <string.h>

//...

strcpy( droneinfo[searchID].drone_name, drname );

【讨论】:

  • 我改了程序,没有报错,但是现在打印出来的数据,并没有保存输入的新无人机信息。
  • @jamisonralph 您需要检查 searchID 是否小于 no_of_drones。
【解决方案2】:
droneinfo[searchID]

类型为drone_info 而不是drone_info*,因此您使用. 而不是-&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-07
    • 2021-12-23
    • 2019-01-26
    • 2016-08-07
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多