1

We know that int *p; means declaring a pointer p to int data and int a,b; means declaring two int variables.

If we consider int * as a data type which means the pointer to int data, then the int*a,b; would means that declaring two pointers.

But it is not consistent with my code result.

This is my code

#include <stdio.h>
int main(void)
{
    int i=10;
    int * a,b;
    a=&i;
    b=&i;
    printf("%d\n",*a);
    printf("%d",*b);
    return 0;
}

The code failed to compile.I removed the * in front of the b and then the code ran successfully.

#include <stdio.h>
int main(void)
{
    int i=10;
    int * a,b;
    a=&i;
    b=&i;
    printf("%d\n",*a);
    printf("%d",b);
    return 0;
}

This seemed that b was not the pointer and it was the int variable.

Since we couldn't see the * as a notation which is independent of p,does this mean that int* p doesn't equal to int *p although the result are same?

Casey
  • 10,297
  • 11
  • 59
  • 88
wit
  • 34
  • 3
  • 1
    `int* a, b;` does not declare two pointers to `int`. Instead it declares a pointer `a` and and integer `b`. If you include the `'*'` with the variable that becomes immediately obvious, e.g. `int *a, b;` – David C. Rankin May 15 '21 at 05:12
  • @DavidC.Rankin the e.g. at the end of your comment is probably a typo, i think you meant to type: int *a, *b; – Harry K. May 15 '21 at 07:11
  • @HarryK., nope, `int *a, b;` was intentional. One pointer-to `int` and one `int`. – David C. Rankin May 15 '21 at 07:51
  • @DavidC.Rankin, ah ok, I thought you wanted to show OP how to declare 2 pointers in the same line (English isn't my 1st language :) ). – Harry K. May 15 '21 at 08:28
  • @HarryK. No worries. Not like I've never misread or confused the issues before ... at least not much more than once daily for good measure. – David C. Rankin May 15 '21 at 09:00

0 Answers0