What is the actual practical difference between int x[] and int *x[] in C?

82 Views Asked by At

Although I've learned and worked with many advanced languages, but today when I was refreshing my C skills this old question again striked me. When we talk about pointers in C, simple pointers are easy to undertand, but for me array pointers didn't make sense (as I could never undertand them).

int x[] = {1, 2, 3};
int *y[] = {4, 5, 6};

In the above code, if I'll do printf("%u", x) it'll give address of first element of x array, which clears the fact that x is a pointer to the array x. Similarly if I'll do printf("%u", y) it'll give address of first element of y array.

So what makes x different from y? Can someone please explain.

P.s. I'm not asking about regular pointers, I'm very well verse with the concept of pointers.

1

There are 1 best solutions below

1
On BEST ANSWER
  1. int x[] = {1, 2, 3}; defines array of 3 integers
  2. int *y[] = {4, 5, 6} defines array of 3 pointers and inializes it with integers converted to pointers which basically makes no sense. In such array you want to store references (pointers) to objects
int a = 4,b = 5,c = 6;
int *y[] = {&a, &b, &c};

In the above code, if I'll do printf("%u", x) it'll give address of first element of x array

No, It invokes Undefined behaviour as you use the wrong format specifiers. You need to printf("%p", (void *)x);