char *p and char p[].

Can anyone please explain me the difference between char *p and char p[] ?

Thanks in Advance,
Arun.

The difference is that 'char *p' is a pointer while 'char p[n]' is an array. So char p will be allocated enough space to hold a memory address that will contain a character type variable, while p[n] will be allocated nsizeof(character type) contiguous memory locations where the data will reside.

If you allocate space using 'char p[10]', then using something like

printf("%c",p[0]);

or

printf("%c",*p);

will give the same output.

there can be numerous differences, depends on what context you want to use this in. The basic difference is as pointed above by blowtorch.

Considering that arrays are also pointers, there's two main differences.

  • Arrays are allocated space. The only reason function arguments get away with leaving the [] empty is they are passed a pointer, which the compiler assumes to have been allocated.
  • Arrays are constant. You can't change their base address.

Other than that, pointers and arrays are nearly identical.

You can think about arrays as constant pointers on automatically allocated memory that cannot be released