无法在C中将结构传递给单独的函数 [英] Trouble passing a struct to a separate function in C

查看:36
本文介绍了无法在C中将结构传递给单独的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将结构传递给新函数时遇到麻烦.我当前的代码打开一个文本文件,将相关信息保存到结构中并打印保存的信息.现在,我正在尝试编写一个函数,该函数将要求用户输入名称,并要求代码检查结构中的所有名称字段并在找到结果后返回inforamtion.在我尝试将结构传递给"searchDroneName"函数之前,代码工作正常.主要显示我已经正确保存了相关信息,并且对"searchDroneName"函数做了完全相同的操作.但是,当我在"searchDroneName"函数中打印出结构的已保存信息时,它将打印出一堆随机数和奇怪的字符.

I'm having trouble passing my struct to a new function. My current code opens a text file, saves the relevant information to the structure and prints the saved information. Now I'm trying to write a function that will ask the user to input a name and for the code to check all name fields in the struct and return inforamtion once found a result. The code works fine until I try pass the struct to the "searchDroneName" function. The main shows that I have saved the relevant information properly and I did the exact same for the "searchDroneName" function. But when I print out the saved information of the struct in the "searchDroneName" function it prints out a bunch of random numbers and weird characters.

我敢肯定,这只是我对功能以及如何传递信息缺乏了解,但是感谢您的帮助.

I'm sure it's just my lack of understanding of functions and how to pass information but and help is appreciated, thanks.

这是代码:

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

typedef struct{

    int drone_number;
    char drone_name[20];
    int year_manufactured;
    double mass;
    double top_speed;
    double max_distance;
    double load_capacity;

} drone_info;


int searchDroneName(int no_of_drones){
    drone_info droneinfo[10];

    int i, found, numdrones;
    char namechoice[20];
    numdrones = no_of_drones;

    // Test Data    
        printf("Data:\n\n");
    for (i=0; i < numdrones; 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);

    }


    printf("Input Drone Name: ");
    scanf("%19s", namechoice);
    found = 0;
    for (i=0; i < numdrones; ++i){
        printf("Drone Name: %s\n", droneinfo[i].drone_name);

        if (!strcmp(namechoice, droneinfo[i].drone_name)){

        printf("FOUND A MATCH");
        found = 1;
        }

    }

    if(found == 0){
        printf("No Matches Were Found!\n");

    }

    return 0;
}






int main(void) {
drone_info droneinfo[10];

int choice, droneID, yrman, i, no_of_drones;
float dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i = 0;
    FILE* inputfile = fopen("drone.txt", "r");
    if(inputfile == NULL)
    {
        perror("ERROR! ");
        exit(-1);
    }

//GAY CODE BELLOW
    while(fscanf(inputfile, "%d %19s %d %f %f %f %f", &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);

    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);

    }

//GAY CODE ABOVE




do{

  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(choice)
  {
    case 1:
    //Input Drone Function

    break;

    case 2:
    //Search Drone function
    searchDroneName(no_of_drones);

    break;

    case 3:
    //Simulate Drone function

    break;

    case 4:
    //Display simulation results function

    break;

    case 5:
    //Save drone information function

    break;

    case 6:
    //Save all results function

    break;

    case 7:
    // Exit, Breaks loop

    break;

    default:
     printf("\nInvalid choice! Please enter a number inbetween 1 and 7!\n\n" );
    break;


  }


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

推荐答案

更改

int searchDroneName(int no_of_drones){drone_info droneinfo [10];

int searchDroneName(drone_info * droneinfo,int no_of_drones){

    case 2:
    //Search Drone function
    searchDroneName(no_of_drones);

    case 2:
    //Search Drone function
    searchDroneName(droneinfo, no_of_drones);

这篇关于无法在C中将结构传递给单独的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆