我正在嘗試爲有關結構的賦值創建程序。這個想法是創建一個包含姓名,電話號碼和電子郵件地址變量的結構。我認爲我的大部分代碼都可以 - 與C的現代編碼標準相比,可能是最基本的代碼,但這是我在課堂上的地方。C:嘗試初始化結構中的char數組時出現賦值錯誤時的不兼容類型
無論如何,我試圖初始化電子郵件地址字段中的5行上出現編譯錯誤,說明賦值中的不兼容類型。但是,我不會在首字母或姓氏字段中看到這些錯誤,但我不明白爲什麼。
任何想法,爲什麼發生這種情況,或與程序其餘部分的錯誤,非常感激!我不能真正調試它的其餘部分,直到我解決了這個編譯錯誤,所以我不確定還有什麼問題。
#include <stdio.h>
#include <string.h>
/*****************************************
Structure declaration, creating type cont
*****************************************/
typedef struct contact {
char fname[20];
char lname[20];
int number[10];
char email[30];
} cont;
/*****************************************
Start of main function
*****************************************/
int main() {
int iMenu; //variable required for the menu
int iStorage; //variable used to store array entry chosen by the user
int iEntry1, iEntry2, iEntry3, iEntry4, iEntry5 = 0; //variables used for flagging assigned entries
/*******************************************
because of the typedef declaration, the struct command
isn't necessary in creating an instance of the structure.
*******************************************/
cont myContact[4];
/*******************************************
we initialize the arrays contained within the structures
*******************************************/
strcpy(myContact[0].fname, "\0");
strcpy(myContact[0].lname, "\0");
myContact[0].number = 0;
strcpy(myContact[0].email, "\0");
strcpy(myContact[1].fname, "\0");
strcpy(myContact[1].lname, "\0");
myContact[1].number = 0;
strcpy(myContact[1].email, "\0");
strcpy(myContact[2].fname, "\0");
strcpy(myContact[2].lname, "\0");
myContact[2].number = 0;
strcpy(myContact[2].email, "\0");
strcpy(myContact[3].fname, "\0");
strcpy(myContact[3].lname, "\0");
myContact[3].number = 0;
strcpy(myContact[3].email, "\0");
strcpy(myContact[4].fname, "\0");
strcpy(myContact[4].lname, "\0");
myContact[4].number = 0;
strcpy(myContact[4].email, "\0");
/*****************************************
Creation of the menu to allow the users
to add entries or view them
*****************************************/
while (iMenu != 3) {
printf("Please select one of the following menu options: \n");
printf("\n1. Input new entries into the phonebook");
printf("\n2. View entries stored in the phonebook");
printf("\n3. Exit the Program\n");
scanf("%d", &iMenu);
/*******************************************
First menu option allows the selection of which
entry, and the storage of phonebook data
********************************************/
if (iMenu == 1) {
printf("Please input the entry in the phonebook you wish to change (0-4): \n");
scanf("%d", iStorage);
printf("Please input the first name of your new contact: \n");
scanf("%s", myContact[iStorage].fname);
printf("Please input the last name of your new contact: \n");
scanf("%s", myContact[iStorage].lname);
printf("Please input the phone number of your new contact: \n");
scanf("%d", myContact[iStorage].number);
printf("Please input the e-mail address of your new contact: \n");
scanf("%s", myContact[iStorage].email);
/**************************************
Nested if statement sets the variable to
flag if an entry has been made
**************************************/
if (iStorage == 0)
iEntry1 = 1;
else if (iStorage == 1)
iEntry2 = 1;
else if (iStorage == 2)
iEntry3 = 1;
else if (iStorage == 3)
iEntry4 = 1;
else if (iStorage == 4)
iEntry5 = 1;
}
/***************************************
Menu option 2 allows the user to display
stored phonebook entries, using the iEntry
variables as flags to determine which ones
to display
***************************************/
else if (iMenu == 2) {
if (iEntry1 == 1)
printf("%s %s phone number: %d e-mail address: %s", myContact[0].fname, myContact[0].lname, myContact[0].number, myContact[0].email);
if (iEntry2 == 1)
printf("%s %s phone number: %d e-mail address: %s", myContact[1].fname, myContact[1].lname, myContact[1].number, myContact[1].email);
if (iEntry3 == 1)
printf("%s %s phone number: %d e-mail address: %s", myContact[2].fname, myContact[2].lname, myContact[2].number, myContact[2].email);
if (iEntry4 == 1)
printf("%s %s phone number: %d e-mail address: %s", myContact[3].fname, myContact[3].lname, myContact[3].number, myContact[3].email);
if (iEntry5 == 1)
printf("%s %s phone number: %d e-mail address: %s", myContact[4].fname, myContact[4].lname, myContact[4].number, myContact[4].email);
}
else if (iMenu > 3) {
printf("Invalid Entry.");
}
}
return 0;
}
cont myContact [4];給你4個內存插槽你如何訪問5個插槽(0到4)和數組是如何分配一個整數到整數陣列 – Civa 2013-03-16 05:24:46