struct之前的预期表达式

时间:2014-08-10 16:20:42

标签: c arrays struct malloc

我的问题是我一直收到错误。我试图显示我的结构数组的元素。我尝试了三种不同的方法,每种都给了我不同的答案。我认为我的问题是我没有正确理解如何指向一个结构数组。指针对我来说只是让人困惑。我的整个代码都在最后。我已经在网上寻找这个问题的答案,但它们是如此多样化,我无法解决它......

重要信息:我加载一个名为entries.txt的输入文件,其内容如下:

姓名 姓 电话号码 等...

第一种方法:直接尝试使用Contact.lname等访问信息。它给了我一个错误“在'联系'之前的预期表达:

    if (choice == 1)
 //      searchIndex();

        {
        printf("Please enter the index of the contact you wish to view. \nThis should be a positive integer\n\n");
//        fgets(vIndex, 700, stdin);
        scanf("%d", &vIndex);
        fgetc(stdin);
        printf("The value of vIndex is %d", vIndex);
        printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. \nPhone Number:\t%c.\n\n ", vIndex, Contact[vIndex].fname, Contact[vIndex].lname, Contact[vIndex].phone);
        }

第二种方法给了我使用联系人的荒谬答案(我意识到这只是为了表明我对该怎么做感到困惑。我虽然出于某种原因这可能有用但是现在我想到了,没有理由它起作用。

  if (choice == 1)
     //      searchIndex();

            {
            printf("Please enter the index of the contact you wish to view. \nThis should be a positive integer\n\n");
    //        fgets(vIndex, 700, stdin);
            scanf("%d", &vIndex);
            fgetc(stdin);
            printf("The value of vIndex is %d", vIndex);
            printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. \nPhone Number:\t%c.\n\n ", vIndex, contacts[vIndex].fname, contacts[vIndex].lname, contacts[vIndex].phone);
            }

我想某种程度上我需要取消引用它,从我读过的和在课堂上教过的' - >'是一个结构数组的解除引用程序,但我需要一些东西指向它,以便它取消引用它。我的完整代码如下。抱歉我的困惑......

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


typedef struct
{
    char fname[20];
    char lname[50];
    char phone[15];
} Contact;



char * stringAlloc(int size);
Contact * contactAlloc();
void structAlloc();

int main(void)
{

    int *ptrInt;
    int i = 0;
    char * myName;
    Contact * contacts;
    ptrInt = (int *)malloc(sizeof(int));
    int vIndex=0;
    int displayMenu();
    Contact * contactAlloc();
    void searchIndex();
    void searchFirst();
    void searchLast();
    void searchPhone();
    Contact * ptrContact;


    contacts = contactAlloc();
    char choice;
    choice = displayMenu();

    while (choice !=5)\
    {
        if (choice == 1)
     //      searchIndex();

            {
            printf("Please enter the index of the contact you wish to view. \nThis should be a positive integer\n\n");
    //        fgets(vIndex, 700, stdin);
            scanf("%d", &vIndex);
            fgetc(stdin);
            printf("The value of vIndex is %d", vIndex);
            printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. \nPhone Number:\t%c.\n\n ", vIndex, Contact[vIndex].fname, Contact[vIndex].lname, Contact[vIndex].phone);
            }

        else if (choice == 2)
            searchFirst();
        else if (choice == 3)
            searchLast();
        else if (choice == 4)
            searchPhone();
        choice = displayMenu();
    }
    printf("Thank for you using this program.\n");
    return 0;
}

int displayMenu()
{

    int choice = 0;
    while (choice!= 1 && choice != 2 && choice != 3 && choice != 4 && choice!=5)
    {
        printf("\nWelcome to the phone book application. Please choose from the following options:");
        printf("\n\n\t 1) Search the phone book by index. \n\t 2) Search the phone book by first name. \n\t 3) Search the phone book by last name. \n\t 4) Search the phone book by phone number. \n\t 5) Quit.\n\n");
        scanf("%d", &choice);
    }
    return choice;
}


Contact * contactAlloc()
{
   FILE * fin;
    int count = 0, i = 0;
    char aLine[100];
    Contact * ptrContact;

    fin = fopen("entries.txt", "r");
    if (fin != NULL)
    {
        while( fgets(aLine, sizeof(aLine), fin) != NULL )
        {
            count++;
        }

        fseek(fin, 0L, SEEK_SET);
        count = count / 3;

        ptrContact = (Contact *) calloc(count, sizeof(Contact));
        count = 0;

        while( fgets(aLine, sizeof(aLine), fin) != NULL )
        {
            if (aLine[strlen(aLine) - 1] == '\n')
            {
                aLine[strlen(aLine) - 1] = '\0';
            }

            if (i % 3 == 0)
            {
                strcpy(ptrContact[count].lname, aLine);
            }
            else if (i % 3 == 1)
            {
                strcpy(ptrContact[count].fname, aLine);
            }
            else if (i % 3 == 2)
            {
                strcpy(ptrContact[count].phone, aLine);
                //printf("Line %d at count %d: %s\n", i, count, aLine);
                count++;
            }
            i++;
        }
        //count=count*3;
        printf("%d contacts loaded.\n\n", count);
        fclose(fin);
    }
    return ptrContact;
}

/*

void searchIndex()
{
    int vIndex=0;
    Contact * ptrContact;
    printf("Please enter the index of the contact you wish to view. This should be a positive integer");
    scanf("%d", vIndex);
    fgetc(stdin);
    printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. Phone Number:\t%c.\n\n ", vIndex, ptrContact[vIndex].fname, ptrContact[vIndex].lname, ptrContact[vIndex].phone);
}

*/
void searchFirst()
{


}

void searchLast()
{



}

void searchPhone()
{


}

0 个答案:

没有答案