简体   繁体   English

将结构传递给函数c

[英]Passing structures to functions c

I'm trying to wite a code that would create an array of structures. 我正在尝试写一个可以创建结构数组的代码。 I'm still at the early stages where I am trying to pass the structure to a function and though I figured pointer would have to be utitlized, I just can't figure out how to do it the correctly. 我仍处于尝试将结构传递给函数的早期阶段,尽管我认为必须使用指针,但是我仍然无法弄清楚如何正确地进行操作。 Here's my code below. 这是我的下面的代码。

#include <stdio.h>

struct age{
    int num;
};

struct name{
    char fname [15];
    char lname[15];
    struct age nameAge;
};

void input(struct name *info[]);

int main (void){
char choice;
char ans;
int i;
struct name record[10];
do{
    printf("M E N U");
    printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
    printf("\n\nEnter choice: ");
    choice=tolower(getche());
    system("cls");

    switch (choice){
        case 'i': input(&record[]);
                  i++;
                break;
        case 'd': //function here
                break;
        case 's': //fucntion here
                break;
        case 'q': printf("The program will now close.");
                break;
        default: printf("Invalid character. Try again");
                break;
        }

    getch();
    system("cls");

   }while (choice!='q');
return 0;
}

void input(struct name *info[]){
//codes here
}

Keep in mind that The name of an array "decays" to a pointer to its first element . 请记住, 数组的名称“衰减”到指向其第一个元素的指针 So, in your case, just use record to pass the "array" (You can't really pass an array) to the function. 因此,在您的情况下,只需使用record即可将“数组”(您不能真正传递数组)传递给函数。 You also need to change the function argument type to struct info* as record is of type struct info* . 您还需要将函数参数类型更改为struct info*因为record的类型为struct info*

Here is the working code with the changes commented: 这是带有注释更改的工作代码:

#include <stdio.h>

struct age{
    int num;
};

struct name{
    char fname[15];
    char lname[15];
    struct age nameAge;
};

/*void input(struct name *info[]);  Note the change in the declaration */
void input(struct name *info);

int main (void){
    char choice;
    char ans;
    int i;
    struct name record[10];
    do{
        printf("M E N U");
        printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
        printf("\n\nEnter choice: ");
        choice=tolower(getche());
        system("cls");

        switch (choice){
            case 'i': input(record); /* Note the change here as well */
                      i++;
                    break;
            case 'd': //function here
                    break;
            case 's': //fucntion here
                    break;
            case 'q': printf("The program will now close.");
                    break;
            default: printf("Invalid character. Try again");
                    break;
            }

        getch();
        system("cls");

       }while (choice!='q');
    return 0;
}

void input(struct name *info){ /* Note the change here in the function definition too */
    //codes here
}

Change the function definition and implementation and the function call like this (if you want to use arrays) 像这样更改函数定义和实现以及函数调用(如果要使用数组)

void input(struct name info[]);
...
           input(record);

Or using pointer (also works with arrays as arguments) 或使用指针(也可以将数组作为参数使用)

void input(struct name *info);
...
           input(record);

You don't want to do this void input(struct name *info[]); 您不想执行此void input(struct name *info[]); for your input function. 用于您的输入功能。 You want to pass a reference to one of the info structs in the array to the function since you only will ever edit one info at a time. 您希望将对数组中信息结构之一的引用传递给函数,因为您一次只能编辑一个信息。

So your altered function would be void input(struct name *info); 因此,您更改的功能将是void input(struct name *info); and you would call it using input(&record[i]); 并且您可以使用input(&record[i]);来调用它 which gives the address of record i. 这给出了记录i的地址。 You also need to initialize i as you never set it to 0, but incitement it. 您还需要初始化i,因为您永远不会将其设置为0,而是将其激发。

another suggestion for you.... 给你的另一个建议。

use a record counter.... - you have to call the routine for each time you want to add a record. 使用记录计数器...。-您每次要添加记录时都必须调用例程。 (I have editted the code so you send the routine a pointer to a single record structure, rather than pass the whole structure). (我已经编辑了代码,因此您向例程发送了指向单个记录结构的指针,而不是传递整个结构)。

#include <stdio.h>

struct age{
    int num;
};

struct name{
    char fname [15];
    char lname[15];
    struct age nameAge;
};

// void input(struct name *info[]);
void input(struct name *info);

int main (void){
char choice;
char ans;
int i;

int n_records=0;

struct name record[10];
do{
    printf("M E N U");
    printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
    printf("\n\nEnter choice: ");
    choice=tolower(getche());
    system("cls");

    switch (choice){
        case 'i': input(&record[n_records]);
                  i++;
                  nrecords++;
                break;
        case 'd': //function here
                break;
        case 's': //fucntion here
                break;
        case 'q': printf("The program will now close.");
                break;
        default: printf("Invalid character. Try again");
                break;
        }

    getch();
    system("cls");

   }while (choice!='q');
return 0;
}

// void input(struct name *info[]){
void input(struct name *info){
//codes here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM