C 结构前应为表达式

C 结构前应为表达式,c,arrays,struct,malloc,C,Arrays,Struct,Malloc,我的问题是我不断地犯错误。我试图显示我的结构数组的一个元素。我试过三种不同的方法,每一种都给了我不同的答案。我认为我的问题是我没有正确理解如何指向结构数组。指针对我来说太混乱了。我的全部代码都在末尾。我已经在网上寻找这个问题的答案,但它们太多样化了,我无法解决它 重要信息:我加载了一个名为entries.txt的输入文件,内容如下: 名字 姓氏 电话号码 等等 第一种方法:直接尝试使用Contact.lname…等访问信息。它在“Contact”之前给了我一个预期表达式的错误: if (

我的问题是我不断地犯错误。我试图显示我的结构数组的一个元素。我试过三种不同的方法,每一种都给了我不同的答案。我认为我的问题是我没有正确理解如何指向结构数组。指针对我来说太混乱了。我的全部代码都在末尾。我已经在网上寻找这个问题的答案,但它们太多样化了,我无法解决它

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

名字 姓氏 电话号码 等等

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

    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()
{


}

我觉得我以前试过,但让我再测试一次……你是对的。。这是一个愚蠢的C而不是S,这是我的问题,。。。哈,谢谢。