【问题标题】:Having error:invalid type argument of unary '*" (have 'int')有错误:一元“*”的类型参数无效(有“int”)
【发布时间】:2021-04-18 08:08:21
【问题描述】:

这是我在这里的第一个问题,我还在学习 c,我正在编写此代码以输入用户银行帐户的详细信息以归档并从文件中读取所有记录,当错误时, 错误:invalid type argument of unary '*" (have 'int') 出现在所有指向 struct *ptr 的指针指向整数值的行上(例如第 40 行)

struct usr_act
{
    char username[24], address[24], status;
    int id, prebal, cp, newbal, pdate;
}a[3];

int j=0;
struct usr_act *ptr;
bool r;

void input()
{
    FILE *fp;
    fp = fopen("accounts.txt","a+");
    
    if(fp==NULL)
    {
        printf("Error!!");
        exit(1);
    }
    
    printf("\n\tRecord %d\n\n",j+1);
    fprintf(fp,"Record",j+1);
    
    printf("\nName:- ");
    for(int i=0;i<24;i++)
    {
        scanf("%c",ptr->username[i]);
    }
    fprintf(fp,"Name:- %s",*(ptr->username));
    
    printf("\nAddress:- ");
    for(int i=0;i<24;i++)
    {
        scanf("%c",ptr->address[i]);
    }
    fprintf(fp,"Address:- %s",*(ptr->address));
    
    printf("\nCustomer Id:- ");
    scanf("%d",ptr->id);
    fprintf(fp,"Customer Id:- %d",*(ptr->id)); //Error 
    
    printf("\nPrevious balance:- ");
    scanf("%d",ptr->prebal);
    fprintf(fp,"Previous Balance:- %d",*(ptr->prebal)); //Error
    
    printf("\nCurrent Payment:- ");
    scanf("%d",ptr->cp);
    fprintf(fp,"Current Payment:- %d",*(ptr->cp)); //Error
    
    printf("\nPayment Date:- ");
    scanf("%d",ptr->pdate);
    fprintf(fp,"Payment Date:- %d",*(ptr->pdate)); //Error
    
    fclose(fp);
    r=true;
}

void calc()
{
    FILE *fp;
    fp = fopen("accounts.txt","a+");
    
    if(fp==NULL)
    {
        printf("Error!!");
        exit(1);
    }
    
    float k;
    k = 0.10 * (*(ptr->prebal));
    
    if(*(ptr->cp)>0 && *(ptr->cp)<k)
    {
        ptr->status='o';
    }
    
    else
    {
        ptr->status='c';
    }
    fprintf(fp,"Account status:- %c",*(ptr->status));
    
    ptr->newbal= *(ptr->prebal) - (*(ptr->cp));
    fprintf(fp,"New Balance:- %d",*(ptr->cp));
    
    fclose(fp);
}

在两者之间我有一个显示功能,它显示文件中的数据,这是主要功能

int main()
{
    int l;
    do
    {
        ptr=&a[j];
        r=false;
        printf("\n\tMenu\nk=1, Input details \nk=2, Show patient records \nk=3, Exit \n\nEnter your choice:- ");
        scanf("%d",&l);
        
        switch(l)
        {
            case 1:
            {
                input();
                calc();
                break;
            }
            
            case 2:
            {
                display();
                break;
            }
        }
        
        if(r==true)
        {
            j=j+1;
        }
    } while(l!=3);
    
    return 0;
}

我该如何解决这个问题?

【问题讨论】:

标签: c printf scanf conversion-specifier


【解决方案1】:

错误与此语句有关

fprintf(fp,"Previous Balance:- %d",*(ptr->prebal));

表达式ptr-&gt;prebal 的类型是int。所以你可能不会应用运算符 *.

还有这个电话

fprintf(fp,"Record",j+1);

没有意义,因为没有使用参数j + 1。

在fprintf 这样的电话中

fprintf(fp,"Name:- %s",*(ptr->username));

参数*(ptr-&gt;username) 的类型无效。而是必须有ptr-&gt;username.

此外,您在 scanf 调用中使用了无效参数,就像在此语句中一样

scanf("%c",ptr->username[i]);

你至少要写

scanf("%c", &ptr->username[i]);

使用前需要阅读fprintf和scanf的说明。

【讨论】:

  • ahhhh fprintf 是一个错误,因为我没有写 %d 但我假设因为 scanf() 必须将地址作为其参数,并且由于指针指向地址,所以我可以按原样写
【解决方案2】:

对于您在第 40 行引用的示例,您需要将参数的地址传递给 scanf(),如下所示:

scanf("%d", &ptr->id);

这是因为scanf() 需要在给定变量中存储一个新值,但由于 C 使用按值传递约定进行参数传递,因此需要指向该变量的指针。

【讨论】:

    【解决方案3】:

    如果您仔细阅读编译器日志,gcc 编译器会按字面意思为您指出错误的确切位置:

    错误:一元'*'的无效类型参数(有'int')

    此消息的意思是“您的一元 '*' 运算符的参数类型无效,您给它一个没有意义的 'int'。然后在错误文本下方您会看到:

    fprintf(fp, "Customer Id:- %d", *(ptr->id));  // Error
                                    ^~~~~~~~~~
    

    这里的^~~~~~~~~~“ASCII art”是一个下划线,指出有问题的表达式*(ptr-&gt;id),^是一个箭头,指出错误的确切位置。

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多