0

Possible Duplicate:
Is array name a pointer in C?

What is the difference between p and a in C?

float a[10],*p; p=a;
Community
  • 1
  • 1

2 Answers2

2

If we define "difference" as the result of subtraction, there answer is zero:

assert((p-a) == 0);

...until you assign some other pointer value to p (which you cannot do with a, because it doesn't name a pointer variable: it names an array which decays to pointer in appropriate contexts; there are other contexts, e.g. sizeof(p)!=sizeof(a)).

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
2
float a[10],*p; p=a;

a is an array 10 of float.

p is a pointer to float. It points to the first element of a.

In C arrays are not pointers. Arrays and pointers are two different types. For example:

sizeof a;   // compute the size of an array
sizeof p;   // compute the size of a pointer

p = &a[1];  // this is valid, p points to the second element of a
a = &p[1];  // this is not valid, you cannot assign to an array
ouah
  • 142,963
  • 15
  • 272
  • 331