Understanding C Pointer Type Error: invalid type argument of 'unary *' (have 'int')

Dec 07, 2025 · Programming · 8 views · 7.8

Keywords: C programming | pointer error | type matching

Abstract: This article provides an in-depth analysis of the common C programming error "invalid type argument of 'unary *' (have 'int')", using code examples to illustrate causes and solutions. It explains the error message, compares erroneous and corrected code, and discusses pointer type hierarchies (e.g., int* vs. int**). Additional error scenarios are explored, along with best practices for pointer operations to enhance code quality and avoid similar issues.

Error Phenomenon and Initial Analysis

In C programming, pointer operations are fundamental but error-prone. A typical error is the compiler message: error: invalid type argument of 'unary *' (have 'int'). This error often occurs when dereferencing a pointer where the operand type is unexpected. For example, in the following code:

#include <stdio.h>
int main(){
    int b = 10;
    int *a;
    a = (int *)&b;
    int *c;
    c = (int *)&a;
    printf("%d", (**c)); // Error occurs here
    return 0;
}

The compiler reports an error at printf("%d", (**c)) because the type of c does not match the dereferencing operation.

Detailed Explanation of the Error

The core issue is a mismatch in pointer type hierarchy. In C, pointer variables store memory addresses, and the dereference operator * accesses the value at that address. When using **c, the compiler expects c to be a pointer to a pointer (e.g., int**), but in the code, c is declared as int*. A step-by-step analysis:

The error message "have 'int'" indicates that during the second dereference, the operand type is int, not a pointer, so the * operator cannot be applied.

Solution and Corrected Code

Based on the best answer, the correction is to declare c as int** to match the type of &a. The corrected code is:

#include <stdio.h>
int main(){
    int b = 10;
    int *a;
    a = &b;
    int **c;
    c = &a;
    printf("%d", (**c)); // Successfully prints 10
    return 0;
}

Key corrections:

  1. Change the declaration of c from int *c; to int **c;, making it a pointer to an integer pointer.
  2. Remove unnecessary type casts (int *), as &a naturally produces type int**, and assignment to c is type-compatible.

After correction, the operation **c proceeds as follows: first dereference *c yields a (type int*), and second dereference **c yields the value of b, 10, allowing the program to run correctly.

Deep Dive into Pointer Type Hierarchies

This error case highlights the importance of pointer type hierarchies in C:

For instance, in the erroneous code, c is misused as int*, while &a produces int**. The cast hides the type error initially but exposes it during dereferencing. This underscores the role of the type system in compile-time checks, which can catch logical errors early.

Other Common Error Scenarios

Referencing other answers, similar errors may occur in different contexts. For example, in C++:

#include <iostream>
using namespace std;
int main(){
    char *p;
    *p = 'c';
    cout << *p[0];  // Error: invalid type argument of 'unary *'
    cout << **p;    // Error: invalid type argument of 'unary *'
    return 0;
}

Here, *p[0] and **p both attempt to dereference non-pointer types because p is char*, p[0] is of type char, and *p yields char, which cannot be dereferenced again. This is analogous to the "opening a stone" metaphor: pointers are boxes, and dereferencing opens the box; if the box contains a stone (non-pointer), it cannot be "opened" further.

Best Practices and Conclusion

To avoid such errors, consider the following best practices:

  1. Clarify Pointer Types: When declaring pointers, carefully consider the data type they point to; use typedef for complex pointer types if necessary.
  2. Avoid Unnecessary Type Casts: Casts can mask type errors, as seen in the original code with (int *)&a; rely on compiler type checking instead.
  3. Use Debugging Tools: Enable compiler warnings like GCC's -Wall -Wextra to identify potential type issues.
  4. Understand Memory Models: Visualize pointer and address relationships using diagrams or debuggers to deepen understanding of hierarchical structures.

In summary, the "invalid type argument of 'unary *'" error typically stems from pointer type mismatches. By declaring types correctly and avoiding casts, developers can write safer, more maintainable C code. Mastering pointer hierarchies is essential for advanced C programming, improving code quality and debugging efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.