LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Error: invalid operands to binary & (have ‘char *’ and ‘int’) (https://www.linuxquestions.org/questions/linux-software-2/error-invalid-operands-to-binary-and-have-%91char-%2A%92-and-%91int%92-4175440073/)

chinabenjamin66 12-05-2012 12:10 AM

Error: invalid operands to binary & (have ‘char *’ and ‘int’)
 
Hello friends,

I am learning C programming under linux recently, this is a example I tried, but got an error.
Code:

1 /* Insurance of driver - using logical operators */
  2 #include <stdio.h>
  3  main()
  4 {
  5        char sex, ms;
  6        int age;
  7        printf("Enter age, sex, marital status");
  8        scanf("%d %c %c" &age, &sex, &ms);
  9        if((ms == 'M') || (ms == 'U' && sex == 'M' && age > 30) || (ms == 'U' && sex == 'F' && age >25))
 10                printf("Driver is insured");
 11        else
 12                printf("Driver is not insured");
 13 }

root@gpl-vm:/lianxi/ch2# gcc -o insure insure.c
insure.c: In function ‘main’:
insure.c:8:19: error: invalid operands to binary & (have ‘char *’ and ‘int’)
root@gpl-vm:/lianxi/ch2# vim insure.c
root@gpl-vm:/lianxi/ch2#

the Linux distribution is

Code:

root@gpl-vm:/lianxi/ch2# cat /proc/version
Linux version 3.2.0-31-generic-pae (buildd@roseapple) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #50-Ubuntu SMP Fri Sep 7 16:39:45 UTC 2012

How to deal with this?

pan64 12-05-2012 12:37 AM

probably you missed a , before &age.
scanf("%d %c %c", &age, &sex, &ms);

TobiSGD 12-05-2012 12:43 AM

Quote:

Originally Posted by chinabenjamin66 (Post 4842976)

How to deal with this?

Simple. The compiler gives you a hint that there is a problem in line 8. If you carefully read that line you will realize that you are missing a comma.

Edit: Too late.

chinabenjamin66 12-05-2012 12:54 AM

Quote:

Originally Posted by TobiSGD (Post 4842993)
Simple. The compiler gives you a hint that there is a problem in line 8. If you carefully read that line you will realize that you are missing a comma.

Edit: Too late.

Thanks, I am a beginer for C programing under linux, some questions I asked may be silly or very simple. hope post valuable question gradually.

pan64 12-05-2012 01:36 AM

great! you are not silly, just made a mistake. Try to interpret the error message and look for strange things (like "binary &")
And press YES (bottom right corner) if you really want to say thanks.


All times are GMT -5. The time now is 05:46 PM.