struct与“long long”类型的参数不兼容

时间:2017-03-17 02:10:06

标签: c function

我这里有一个用C编写的简单电话簿应用程序,用于实验室作业。我遇到了麻烦,但它没有编译 - 我收到错误“struct PhoneBook_Contacts”与“long long”类型的参数不兼容。我不确定如何更改或修复此问题,我是C的完全初学者。错误在第37行。如果您能提供帮助,请告诉我。我知道错误是什么,而不是如何解决它。如果可以,请让我知道在什么线上确切改变:D

提前致谢

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

#define SIZE 3

void decompose(long long phone_number, int *area, int *prefix, int *lineno);

void decompose(long long phone_number, int *area, int *prefix, int *lineno) {
    *area = (int)(phone_number / (10000000));
    *prefix = (int)((phone_number % (10000000) / 10000));
    *lineno = (int)(phone_number % (10000));
}


struct PhoneBook_Contacts
{
    char FirstName[20]; //Entered First Name
    char LastName[20]; //Entered Last Name
    char PhoneNumber[20]; //Phone Number
}; //TypeDef to Modify structure name



   //Begin main function
int main(void)
{
    int area, prefix, lineno;
    int counter = 0; 
    int iSelection = 0; //Variable to use to select menu choice//Global counter variable used to keep track of number of contacts
                     //phone *phonebook; //Phonebook instance
                     //phonebook = (phone*)malloc(sizeof(phone) * 1); //Allocate memory for contacts
    struct PhoneBook_Contacts phonebook[3];



    printf("---=== Phone Numbers ===---\n\n");


    while (iSelection <= 4)
    {

        printf("1. Display Phone List\n");
        printf("2. Add a Number\n");
        printf("0. Exit\n\n");
        printf("Please select from the above options: ");
        scanf("%d", &iSelection);
        printf("\n");

        // Add Friend
        if (iSelection == 2)
        {
            if (counter == 3) {
                printf("ERROR!!! Phone Number List is Full\n");
                printf("\n");
            }
            else {
                printf("Add a Number\n");
                printf("============\n");
                scanf("%s", phonebook[counter].PhoneNumber);
                printf("\n");
                counter++;
            }
        } //End if
          //printf("%d\n", iSelection);
          //Print Phonebook Entries
        if (iSelection == 1)
        {
            int x = 0;
            printf("Phone Numbers\n");
            printf("==============\n");
            for (x = 0; x < counter; x++) //For loop to print entries
            {
                decompose(phonebook[x], &area, &prefix, &lineno);
                printf("%s\n", phonebook[x].PhoneNumber); //Contact's Phone Number
                printf("%d%d%d", area, prefix, lineno);
                printf("\n");
            }
            //printf("%d%d%d", area, prefix, lineno);
            //printf("\n");

            //End for loop
        } //End if

          //Exit Application
        if (iSelection == 0)
        {
            printf("Exiting Phone Number App. Good Bye!!!\n");
            break;
        } //End if

    } //End while
    return 0;
} //End main function

2 个答案:

答案 0 :(得分:1)

第一个参数的类型是long long

void decompose(long long phone_number, int *area, int *prefix, int *lineno)

这不长,是一个结构PhoneBook_Contacts

struct PhoneBook_Contacts phonebook[3];

所以你不能使用类似电话簿的参数。

decompose(phonebook[x], &area, &prefix, &lineno);

但这没关系。

void decompose(struct PhoneBook_Contacts phone_number, int* area, int* prefix, int* lineno)

你也有问题。第一,因为你需要使用phone_number.PhoneNumber第二,因为PhoneNumber是char而不是int,而第三......那是给你的......

答案 1 :(得分:0)

刚刚编译宝贝。编译......并在打印数据时解决一个问题。

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

#define SIZE 3

struct PhoneBook_Contacts
{
    char FirstName[20]; //Entered First Name
    char LastName[20]; //Entered Last Name
  long unsigned PhoneNumber[20]; //Phone Number
}; //TypeDef to Modify structure name

void decompose(long unsigned number, int* area, int* prefix, int* lineno) {
  *area = (number / (10000000));
  *prefix = ((number % (10000000) / 10000));
  *lineno = (number % (10000));
}

//Begin main function
int main(void)
{
    int area, prefix, lineno;
    int counter = 0; 
    int iSelection = 0; //Variable to use to select menu choice//Global counter variable used to keep track of number of contacts
                     //phone *phonebook; //Phonebook instance
                     //phonebook = (phone*)malloc(sizeof(phone) * 1); //Allocate memory for contacts
    struct PhoneBook_Contacts phonebook[3];



    printf("---=== Phone Numbers ===---\n\n");


    while (iSelection <= 4)
    {

        printf("1. Display Phone List\n");
        printf("2. Add a Number\n");
        printf("0. Exit\n\n");
        printf("Please select from the above options: ");
        scanf("%d", &iSelection);
        printf("\n");

        // Add Friend
        if (iSelection == 2)
        {
            if (counter == 3) {
                printf("ERROR!!! Phone Number List is Full\n");
                printf("\n");
            }
            else {
                printf("Add a Number\n");
                printf("============\n");
                scanf("%lu", phonebook[counter].PhoneNumber);
                printf("\n");
                counter++;
            }
        } //End if
          //printf("%d\n", iSelection);
          //Print Phonebook Entries
        if (iSelection == 1)
        {
            int x = 0;
            printf("Phone Numbers\n");
            printf("==============\n");
            for (x = 0; x < counter; x++) //For loop to print entries
            {
          decompose(*phonebook[x].PhoneNumber, &area, &prefix, &lineno);
                printf("%lu\n", *phonebook[x].PhoneNumber); //Contact's Phone Number
                printf("%d%d%d", area, prefix, lineno);
                printf("\n");
            }
            //printf("%d%d%d", area, prefix, lineno);
            //printf("\n");

            //End for loop
        } //End if

          //Exit Application
        if (iSelection == 0)
        {
            printf("Exiting Phone Number App. Good Bye!!!\n");
            break;
        } //End if

    } //End while
    return 0;
} //End main function