【问题标题】:Passing Arrays of Structures to User-defined Functions in C将结构数组传递给 C 中的用户定义函数
【发布时间】:2021-09-04 03:21:06
【问题描述】:

我对在 C 编程中传递数组结构的概念感到困惑。大多数示例涉及传递具有已提供值的结构数组。我不得不思考、重新阅读,当我看到有人接受输入并将结构成员放入每个单独的元素时,它点击了。它让我的大脑动了起来,我创建了下面的程序。我需要通过用户定义的函数推送结构数组。我让它可以在 CodeBlocks 上工作,但不能在 Cygwin 上工作。随时欢迎输入。

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

#define NAME 50
#define PHONE 10
#define MAX 5

// Declare a global structure to pass
// it to a function
struct contact{
    char name[NAME];
    char phone[PHONE];
};

//Array of structure
struct contact * friend[MAX]= {0};

//Declaration of the function
void loadMenu(void);
void insertEntry(struct contact *friend);
void displayEntry(struct contact *friend);

int main(){

    loadMenu();
    return 0;
}

void loadMenu(){

    int choice;

    
        printf("\t\tWelcome to Your PhoneBook \n");
        printf("\t\n 1> Insert new Entry");
        printf("\t 2> Display Entry");
        printf("\t 3> Exit\n");
        printf("\n Enter your choice <1-3>: ");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                insertEntry(friend);
                break;
            case 2:
                displayEntry(friend);
                break;
            case 3:
                exit(0);
                break;
            default:
                printf("**Invalid Input.**\n");
        }//end switch

} //end loadMenu

void insertEntry(struct contact *friend){
    char choice1;
    FILE *pWrite;
    int i;

    do{
    if(i == 0){
        //opening the phoneBook file
    pWrite = fopen("phoneBook.txt", "w");
    } else {
        pWrite = fopen("phoneBook.txt", "a+");
    }

        // Make sure do not enter more than MAX number of records
        if(i >= MAX){
        printf("Reached maximum number of entries that can be filled\n");
        break;
        }

        while (1){
            printf("Enter friend's Name\n");
            scanf("%s", friend[i].name);
            if(strlen(friend[i].name) == 0){
                printf("Name cannot be empty\n");
            } else {
                break;
            }
        }

        while (1){
            printf("Enter friend's Phone number\n");
            scanf("%s", friend[i].phone);
            if(strlen(friend[i].phone) == 0){
                printf("Phone number cannot be empty\n");
            } else {
                break;
            }
        }

        fprintf(pWrite, "%s\t%s \n", friend[i].name, friend[i].phone);
        i++;

    fclose(pWrite);

        fflush(stdin);

        printf("Do you to insert another entry? Y or N \n");
        scanf("%c", &choice1);
        } while (choice1 == 'y' || choice1 == 'Y');
    loadMenu();

}// end insertEntry
void displayEntry(struct contact *friend){

   //Print the phone book's valid current entries. Do not display phone book entries that are invalid or NULL (0).
    FILE *pRead;
    int i;
    char reRun;

     do{
    pRead = fopen("phoneBook.txt", "r");
    if (pRead != NULL){
        printf("\nName\t Phone Number\n\n");
        fscanf(pRead, "%s %s", friend[i].name, friend[i].phone);
        while (!feof(pRead)){
        printf("%s\t%s\n", friend[i].name, friend[i].phone);
        fscanf(pRead, "%s%s", friend[i].name, friend[i].phone);
        }

        fclose(pRead);}
    else{
        printf("\n File not opened\n");
    }
      fflush(stdin);

        printf("Press Y to Execute the Program Again \n");
        scanf("%c", &reRun);
        loadMenu();
        } while (reRun == 'y' || reRun == 'Y');

}//end displayEntry

【问题讨论】:

  • 有两个版本的代码令人困惑。它使问题变得没有重点,难以理解你真正在问什么。请只显示一个不工作的代码版本。然后给出输入、预期结果和实际结果。
  • 抱歉,我将编辑我的帖子以获得一个代码。
  • 每次运行时都在创建数组。在全局命名空间或 main 中只创建一次,然后将指向该数组的指针指向将处理该数据的每个函数。
  • 我看不到您在代码中的任何位置传递对数组或结构的引用。 Nitpick:如果你的感觉太敏感以至于无法写出 shitter,那么将 i 替换为 * 并不会真正愚弄任何人。为什么不找一个不同的词,比如 breaks
  • @Caleb 很沮丧。我做了细致的笔记——我在理论上理解,阅读 stackoverflow,阅读教程,阅读 2005 年的论坛。我一直坚持基础知识,但它不起作用。我使用 Cygwin 和 Codeblocks 来仔细检查我的代码。我不确定我遗漏了哪一部分。

标签: c struct user-defined-functions pass-by-reference


【解决方案1】:

您的问题是您根本没有将任何东西传递给您的各种功能。看看这个:

void insertEntry( ) {
    struct contact * con[MAX]= {0};

这意味着,每当调用insertEntry() 时,它都会创建一个局部变量con,它是一个指向contact 结构数组的指针。当该函数退出时,poofcon 消失,其中的任何内容都消失了。您的其他功能也一样,例如:

void displayEntry(){
    struct contact * con[MAX]= {0};

您要做的是创建该数组一次,然后将其作为参数传递给您的函数。函数名称末尾的 () 用于参数。所以你可能有:

void insertEntry(struct contact * con) {
    int i = 0;
    //...

main() 函数中创建数组:

struct contact * friends[MAX]= {0};

然后当您调用insertEntry 时将其作为参数包含:

insertEntry(friends);

(我称数组friends 只是为了给它一个与参数不同的名称。)这里的想法是insertEntry 不应该创建自己的数组,而是使用你告诉它的那个关于。

顺便说一句,您可以通过使用typedef 为类型struct contact * 命名来清理代码。我暂时不谈这个,但你肯定会在你的教科书中找到如何做到这一点的例子。

【讨论】:

  • 谢谢。教程和我的笔记让它看起来很容易,我写下了问题的所有主要组成部分,但执行起来就不一样了。我的伪代码并不像它需要的那样具体......我会重新阅读。它没有点击,示例不显示输入通过结构传递数组的程序。它显示了一个数组正在通过一个分配了值的函数。
  • 我一直在阅读和查看各种教程。我让它通过函数并添加了文件处理。它适用于代码块,但不适用于 Cygwin。我会把它当作胜利。
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多