【问题标题】:passing 'person [4]' to parameter of incompatible type 'person'将“人 [4]”传递给不兼容类型“人”的参数
【发布时间】:2021-07-27 15:12:13
【问题描述】:

这里我正在尝试在下面编写一个 c 编程,这里我创建了一个数据类型 person,它有两个参数 name 和 number(创建候选人的姓名和他们收到的选票),以及当提示输入时,如果用户在 person data_type 数组中输入名称并且数字对应于该数字应该增加,但我陷入了下面的这个错误。

#include<stdio.h>
#include<cs50.h>
#include<string.h>
//Making person variable
typedef struct
{
    string name;
    int number;

}
person;

int update_vote(string name,person arr);

int main(void)
{
    person cand[4];
    cand[0].name="Brian";
    cand[1].name="David";
    cand[2].name="Obama";
    cand[3].name="Biden";
    
    cand[0].number=0;
    cand[1].number=0;
    cand[2].number=0;
    cand[3].number=0;
    
    //print the candidate names on the screen
    float len=sizeof(cand)/sizeof(cand[0]);
    int yu =(int) len;
    printf("Candidates Who are participating:");
    printf("\n");
    for (int i=0;i<yu;i++)
    {
      printf("%i.%s ",i+1,cand[i].name); 
    }
    printf("\n");
    
    //prompt for voters and take votes and update
    int voters=get_int("Enter the number of voters:");
    
    for (int j=0;j<voters;j++)
    {
        string vote=get_string("Enter your vote:");
        int update=update_vote(vote,cand);
        if (update!=-1)
        {
            cand[update].number++;
        }
        else{
            printf("Invalid name entered");
        }
    }
    
    printf("%d",cand);
    
}

int update_vote(string name,person arr)
{
     float len=sizeof(arr)/sizeof(arr[0]);
     int yu =(int) len;
     for(int i=0;i<yu;i++)
     {
         if (strcmp((name,arr[i].name)==0))
         {
             return i;
         }
     }
     return -1;
}

但一直在弄清楚为什么会出现以下错误

错误:将“person [4]”传递给不兼容类型“person”的参数 int update=update_vote(vote,cand);

【问题讨论】:

  • 没有小数长度数组。 len 应该是 int 或 size_t。

标签: c compiler-errors cs50


【解决方案1】:

错误是因为您传递了person*,其中person 应作为参数。

您应该更改参数的类型,以便它可以接收(指向)数组。

还要注意sizeof cannot be used to determing the number of elements of arrays passed as arguments。您应该分别传递元素的数量。

另一点是printf("%d",cand); 将调用未定义的行为,因为传递了错误类型的数据。 %d 期望 int。如果要打印指针,应将其转换为void* 并使用%p 格式说明符。

试试这个:

#include<stdio.h>
#include<cs50.h>
#include<string.h>
//Making person variable
typedef struct
{
    string name;
    int number;

}
person;

int update_vote(string name,person* arr,int arrSize); /* fix arguments */

int main(void)
{
    person cand[4];
    cand[0].name="Brian";
    cand[1].name="David";
    cand[2].name="Obama";
    cand[3].name="Biden";
    
    cand[0].number=0;
    cand[1].number=0;
    cand[2].number=0;
    cand[3].number=0;
    
    //print the candidate names on the screen
    float len=sizeof(cand)/sizeof(cand[0]);
    int yu =(int) len;
    printf("Candidates Who are participating:");
    printf("\n");
    for (int i=0;i<yu;i++)
    {
      printf("%i.%s ",i+1,cand[i].name); 
    }
    printf("\n");
    
    //prompt for voters and take votes and update
    int voters=get_int("Enter the number of voters:");
    
    for (int j=0;j<voters;j++)
    {
        string vote=get_string("Enter your vote:");
        int update=update_vote(vote,cand,yu); /* pass the number of elements */
        if (update!=-1)
        {
            cand[update].number++;
        }
        else{
            printf("Invalid name entered");
        }
    }
    
    printf("%p",(void*)cand); /* use correct way to print a pointer */
    
}

int update_vote(string name,person* arr,int arrSize) /* fix arguments */
{
     int yu =arrSize; /* use the passed size instead of sizeof */
     for(int i=0;i<yu;i++)
     {
         if (strcmp((name,arr[i].name)==0))
         {
             return i;
         }
     }
     return -1;
}

【讨论】:

  • 非常感谢先生
  • 如果您不介意,请您解释一下问题的原因。
  • @Omkar 我已经解释过了:arr 参数的类型是错误的。
  • 是的,但我没听懂你说的,是不是因为我们应该在使用在该程序中本地创建的数据类型时使用 * 符号?
  • @Omkar 否。通常some_type 类型的参数无法接收some_type 的数组。数组(通常)作为指向它们在 C 中的第一个元素的指针传递。因此,您将需要指针 some_type* 的参数。
【解决方案2】:

因为 person cand[4]; ,当你使用“ int update=update_vote(vote,cand);” , 表示 'person *' 是指针。

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2015-06-07
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2016-01-27
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多