Invalid type argument of unary * (have int) Error in C
Invalid type argument of unary * (have int) Error in C
You forgot to make p
and q
int
pointers. Also, you forgot to use the format specifier in the printf
statements. Try the following:
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
int a[] = {5, 15, 34, 54, 14, 2, 52, 72};
int *p = &a[1];
int *q = &a[5];
printf(%dn, *(p+3));
printf(%dn, *(q-3));
printf(%dn, *q-*p);
printf(%dn, *p<*q);
return (EXIT_SUCCESS);
}
&a[3]
(or &a[5]
) is a pointer type, i.e. int *
.
p
is defined as int
.
So you need to define p
and q
as int *
, like this:
int * p = &a[1];
int * q = &a[5];
Invalid type argument of unary * (have int) Error in C
The unary operator &
yields the address of its operand. The type is of T *
, not T
. Therefore you cannot assign a int *
to an int
without a cast. The expression
&a[1]
yields the address of a[1]
.
I think you mean to define the variables as pointers to int, not just ints.
int *p = &a[1];
int *q = &a[5];
Related posts on unary :
- c# – IEnumerable vs List – What to Use? How do they work?
- python – Create dynamic URLs in Flask with url_for()
- BASH Syntax error near unexpected token done
- python – Usage of sys.stdout.flush() method
- python – Bad operand type for unary +: str
- c++ – error: lvalue required as unary & operand
- Invalid type argument of unary * (have int) Error in C
- R: Unary operator error from multiline ggplot2 command
- c++ – to_string is not a member of std, says g++ (mingw)