Compilation (gcc error)Type & Declaration5 min read

Fix “invalid type argument of unary '*'” in C

The unary indirection operator `*expr` requires its operand to be a pointer type. When GCC or Clang reports `invalid type argument of unary '*' (have 'int')`, your code is attempting to dereference an ordinary non-pointer value—commonly caused by writing `*arr[i]` on a plain `int arr[]`, double-dereferencing a single pointer `**p`, or forgetting parentheses around `*(ptr + i)` vs `*ptr.field`.

Quick Fix Summary

Check the declared type of the expression under *: remove the redundant * when indexing an ordinary array (arr[i] already dereferences the element), change the parameter declaration to int *p if pass-by-address was intended, or use ptr->field instead of *ptr.field.

Why This Error Happens

Why arr[i] and *p already produce values, not pointers

In C, arr[i] is defined as *(arr + i). Putting another * in front of arr[i] when arr is int arr[4] asks the compiler to dereference an int, triggering have 'int'.

Postfix . binds tighter than prefix *

Writing *node.next parses as *(node.next). If node is a struct Node *, . fails first or * applies to the wrong type—write node->next or (*node).next.

Top Root Causes & Specific Code Fixes

Dereferencing an array element that is already an int

Indexing `nums[0]` yields an `int`, so `*nums[0]` tries to dereference an integer.

Broken Pattern
int nums[3] = {10, 20, 30};
printf("%d\n", *nums[0]);
Corrected Pattern
int nums[3] = {10, 20, 30};
printf("%d\n", nums[0]);
Illustrative diagnostic (not executed):error: invalid type argument of unary '*' (have 'int')

Forgetting pointer syntax in a swap/out-parameter signature

Declaring `void set_zero(int x)` instead of `int *x` makes `*x = 0` invalid.

Broken Pattern
void set_zero(int x) {
    *x = 0;
}
Corrected Pattern
void set_zero(int *x) {
    *x = 0;
}

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
#include <stdio.h>
int main(void) {
    int scores[3] = {85, 90, 95};
    printf("First: %d\n", *scores[0]);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
int main(void) {
    int scores[3] = {85, 90, 95};
    printf("First: %d\n", scores[0]);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

First: 85

Debugging Techniques and Commands

Inspect Exact GCC Type
gcc -std=c17 -Wall -Wextra main.c -o app

Read the `(have '...')` suffix in GCC output to see the exact non-pointer type.

Arrow Operator for Struct Pointers
clang -std=c17 -Wall main.c -o app

Use `ptr->member` rather than `*ptr.member` to avoid precedence bugs.

Frequently Asked Questions

Why does *scores work while *scores[0] fails?

An array expression `scores` decays to `int *` pointing to element 0, so `*scores` is `int`. By contrast, `scores[0]` is already `int`, leaving no pointer left to dereference.

What if GCC says have double or have struct Point?

The rule is identical: only pointer types may follow unary `*`. Pass `&value` if a function needs a pointer, or access struct fields directly with `.`.

How is unary * different from multiplication *?

Binary `a * b` multiplies two arithmetic operands, whereas unary `*p` has a single operand and dereferences a pointer. Missing an operand before `*` can accidentally turn multiplication into unary `*`.