What and why is wrong in these ads? (Ads are independent)

int var; int *iptr =&ivar; int ivar, *iptr=&ivar; float fvar; int *iptr=&fvar; int nums[50], *iptr=nums; int ivar, *iptr; *iptr = &ivar; 

Thank.

    1 answer 1

    int var; int * iptr = & ivar;

    here it is commonplace - ivar not declared.

     int ivar, *iptr=&ivar; 

    here, except the use of the uninitialized variable ivar .

     float fvar; int *iptr=&fvar; 

    different types - pointer to integer and pointer to real.

     int nums[50], *iptr=nums; 

    nums is actually a pointer, it will compile. And it will work. But apparently it means that there are "different types".

     int ivar, *iptr; *iptr = &ivar; 

    Here *iptr is already a name. *iptr is an integer type. And trying to assign an address.

    • 2
      Comma is a good learning example for attentiveness. int ivar, * iptr = & ivar; At first it seemed to me that this is dereference, and not an announcement. int nums [50], * iptr = nums; This is an implicit type conversion nums = & nums [0]. But it works. PS Do not declare pointers in one line with non-pointers in real code. This reduces readability. - igumnov
    • 2nd and 4th lines (if they are independent) without errors (both in C ++ and C). In other errors, as @KoVadim wrote - avp