0

This is my first question on here and im still learning c, and i was writing this code to enter details of a user bank account to file and reading all records back from file, when the error, error:invalid type argument of unary '*" (have 'int') appeared on all lines that had pointer to struct *ptr pointed to integer values (eg line 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);
}

and in between i have a display function which displays data from file and this is the main function

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;
}

How can i solve this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
silvercen
  • 11
  • 3
  • Check if this solves your question https://stackoverflow.com/questions/42875580/invalid-type-argument-of-unary-have-int-error-in-c. Basically you are trying to print the pointer and not the value – Dobromir Velev Jan 13 '21 at 11:08
  • Does this answer your question? [Invalid type argument of unary '\*' (have 'int') Error in C](https://stackoverflow.com/questions/42875580/invalid-type-argument-of-unary-have-int-error-in-c) – the busybee Jan 13 '21 at 11:52

3 Answers3

0

For the example you cited on line 40, you need to pass the address of the argument to scanf() like this:

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

This is because scanf() will need to store a new value in the given variable, but because C uses a pass by value convention for argument passing, the pointer to the variable is needed.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

The error has a relation to this statement

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

The type of the expression ptr->prebal is int. So you may not apply the operator *.

Also this call

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

does not make a sense because the argument j + 1 is not used.

In calls of fprintf like this

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

there is an invalid type of the argument *(ptr->username). Instead there must be ptr->username.

Also you are using invalid arguments in calls of scanf like in this statement

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

You have at least to write

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

You need to read the descriptions of fprintf and scanf before using them.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • ahhhh the fprintf was a mistake cos i didnt write %d but i assumed since scanf() has to have the address as its arguments and since pointer points to the address i could just write it as it is – silvercen Jan 13 '21 at 14:27
0

The gcc compiler literally points out the exact location of the bug for you, if you read the compiler log carefully:

error: invalid type argument of unary '*' (have 'int')

This message is meant to say "your argument to the unary '*' operator has invalid type, you have given it an 'int' which doesn't make sense. Then below the error text you get this:

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

Here the ^~~~~~~~~~ "ASCII art" thing is meant to be an underline pointing out the offending expression *(ptr->id) and the ^ is meant to be an arrow pointing out the exact location of the bug.

Lundin
  • 195,001
  • 40
  • 254
  • 396