1
#include <stdio.h>

void main()
{
    int array[5]={1,2,3,4,5};
    int *parray;
    *parray=&array;
    int i;
    for(i=0;i<5;i++)
    {
        printf("%d%d",*(parray[i]*2));// [Error] invalid type argument of unary '*' (have 'int')
    }
    getch();
}

I don't know why this error is coming. How do I solve it?

W.Ali
  • 45
  • 7

2 Answers2

3
  1. This line

    *parray=&array;
    

    is undefined behavior because parray hasn't been initialized. Dereferencing it will point to an indeterminate memory location.
    Use

    parray = array;
    

    instead. Read up on the difference between array and &array also.

  2. This expression

    *(parray[i] * 2)
    

    causes your compilation error. Applying the subscript operator to an int* (parray) yields an int. Multiplying that by 2 yields an int again. Now you attempt to dereference that int. int is no pointer, so it cannot be dereferenced.

    The format specifier %d%d expects two ints. You only provide one, so this is undefined behavior again. A simple %d suffices.

    After all, change

    printf("%d%d",*(parray[i]*2));
    

    to

    printf("%d", parray[i] * 2);
    
Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
2
int *parray;
*parray=&array;

1. parray is of type int * and in next line when you dereference it (undefined behaviour as it is uninitialized) becomes type int to which you try to assign address of array . Just do -

parray=array;

2. And in this line -

  printf("%d%d",*(parray[i]*2));  //gave 2 specifiers but pass one argument ??

You use 2 format specifiers but you pass only one argument and that also of wrong type.

When you write parray[i] it means *(parray+i) ,you don't need to further dereference it using * , as it already of type int (and you try to dereference an int variable).

Just writing this would work-

printf("%d",parray[i]*2);
ameyCU
  • 16,489
  • 2
  • 26
  • 41