-1

Are the following same:

extern int a[];

and

extern int *a;

I mean, are they interchangable?

b4hand
  • 9,550
  • 4
  • 44
  • 49
chanzerre
  • 2,409
  • 5
  • 20
  • 28
  • possible duplicate of [Changing the value of an external variable](http://stackoverflow.com/questions/11433953/changing-the-value-of-an-external-variable) – Oliver Charlesworth Sep 01 '13 at 13:47
  • May be this help you: [Difference between `char *str` and `char str[]` and how both stores in memory?](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Sep 02 '13 at 04:34

4 Answers4

2

No they are not. You'll see the difference when you try something like a++.

There's plenty of questions about the difference between pointers and arrays, I don't think it's necessary to write more here. Look it up.

nickie
  • 5,608
  • 2
  • 23
  • 37
1

They are not the same. The first:

extern int a[];

declares an array of int. The second

extern int *a;

declares a pointer to int. As the C FAQ states:

The array declaration "char a[6]" requests that space for six characters be set aside, 
to be known by the name "a". That is, there is a location named "a" at which six 
characters can sit. The pointer declaration "char *p", on the other hand, requests a 
place which holds a pointer, to be known by the name "p". This pointer can point 
almost anywhere: to any char, or to any contiguous array of chars, or nowhere.

This causes a difference in how the compiler behaves:

It is useful to realize that a reference like "x[3]" generates different code 
depending on whether "x" is an array or a pointer. Given the declarations above, when 
the compiler sees the expression "a[3]", it emits code to start at the location "a", 
move three past it, and fetch the character there. When it sees the expression "p[3]", 
it emits code to start at the location "p", fetch the pointer value there, add three 
to the pointer, and finally fetch the character pointed to. In other words, a[3] is 
three places past (the start of) the object named a, while p[3] is three places 
past the object pointed to by p. 

You'll have unexpected behavior if you use extern int *a if a is actually an array. Given the expression a[3], the compiler will treat the first element of a as an address and try to get the element three places past that address. If you're lucky, the program will crash; if you're not, some data will be corrupted.

verbose
  • 7,827
  • 1
  • 25
  • 40
0
extern int a[];

is an array object

extern int *a;

is a pointer that could be pointed to an array of int

See the above links for more information concerning difference between pointer and array object

*(a++) is giving error but not *(a+1)?? where a is array name?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • You mean pointed to the integer that is first element of an array, right? Not array itself? Since array is an adress of int itself... – zubergu Sep 01 '13 at 14:03
0

When you see extern int a[] it tells you that somewhere (may be in other file) is declared an array of integers, while extern int * a tells you that you have pointer to an integer declared somewhere else and you wish to use it here. There are more differences but that would be another topic.

zubergu
  • 3,646
  • 3
  • 25
  • 38