• Register
search
Log In

Errors are the mistakes or faults in the program that cause our program to behave unexpectedly and it is no doubt that the well versed and experienced programmers also make mistakes.Sometimes the cause of the error is small enough to be ignored and this silly mistake of ours leads the program to the error. In this article we will discuss the cause and the solution of the error “invalid type argument of unary ‘*’ (have ‘int’)”.

 

Example

Here is the program to print the array by filling the array using the pointer and having the same error.

#include <iostream>

using namespace std;

void table_print(int array, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << array[i] << " " << endl;

    }
}

void table_fill(int p, int size, int num)
{
    for (int i = 0; i < size; i++)
    {
        *p = num;
        p++;
    }
}

int main()
{
    const int MAX = 20;
    int *p, *s, *tp;

    int *ary, *bry;
    ary = new int[MAX];
    bry = new int[MAX];
    p = ary;
    s = bry;

    table_fill(p, MAX, 20);
    cout << endl << "Fill array with 20s " << endl;
    table_print(p, MAX); cout << endl << endl;

    return 0;
}

Cause

Notice this line in the example 

void table_print(int array, int size)

Here we have defined the array as integer inside the table_print function. And in the int main(), we are providing a pointer as argument. Hence it is invalid and causes an error. The same problem is causing the error in function table_fill. 

 

Solution

To resolve this issue, we just have to replace the integer argument with a pointer argument. And make sure that you don’t forget to create a pointer this time. Do it like this 

void table_print(int* array, int size)

 

posted Jun 26 in c++ 100 points