【问题标题】:Passing structures to functions c将结构传递给函数 c
【发布时间】:2016-01-14 16:16:36
【问题描述】:

我正在尝试编写一个可以创建结构数组的代码。我仍处于尝试将结构传递给函数的早期阶段,虽然我认为必须使用指针,但我只是不知道如何正确地做到这一点。下面是我的代码。

#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
}

【问题讨论】:

  • 为什么input(&amp;record[]);??
  • 旁白:你需要#include 更多的库头文件——至少ctype.hconio.hstdlib.h。您应该收到编译器警告。而char choice; 应该是int choice;getchetolower 的返回类型相同。
  • 注意:你还没有初始化i

标签: c arrays function structure


【解决方案1】:

请记住,数组的名称“衰减”为指向其第一个元素的指针。因此,在您的情况下,只需使用 record 将“数组”(您不能真正传递数组)传递给函数。您还需要将函数参数类型更改为struct info*,因为record 的类型为struct info*

这里是带有更改注释的工作代码:

#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
}

【讨论】:

    【解决方案2】:

    像这样更改函数定义和实现以及函数调用(如果你想使用数组)

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

    或者使用指针(也可以使用数组作为参数)

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

    【讨论】:

      【解决方案3】:

      你不想为你的输入函数做这个void input(struct name *info[]);。您希望将对数组中的一个信息结构的引用传递给函数,因为您一次只能编辑一个信息。

      所以你改变的函数是void input(struct name *info);,你可以使用input(&amp;record[i]);来调用它,它给出了记录i的地址。您还需要初始化 i 因为您从未将其设置为 0,而是将其设置为 0。

      【讨论】:

        【解决方案4】:

        给你的另一个建议....

        使用记录计数器.... - 每次要添加记录时都必须调用例程。 (我已经编辑了代码,因此您可以向例程发送一个指向单个记录结构的指针,而不是传递整个结构)。

        #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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-06
          • 2021-03-17
          • 2013-04-12
          • 2015-01-03
          • 2012-10-06
          • 1970-01-01
          相关资源
          最近更新 更多