【发布时间】: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