0

Whats the difference between last 2 statements:

int a[20];
int *b=a;
int *c=&a;

I think both are same, but in a recent interview the interviewer was keen to know the difference which I didn't know.

Can somebody please explain with detail example.

I went to this post but didn't understand the array related part: Function pointers and address of a function

Community
  • 1
  • 1
user2333014
  • 157
  • 1
  • 1
  • 4
  • 1
    Duplicate hundreds of times over. – Carl Norum May 07 '13 at 03:22
  • 1
    Example: [Difference between pointer to pointer and pointer to array?](http://stackoverflow.com/questions/6474104/difference-between-pointer-to-pointer-and-pointer-to-array) Read the [C FAQ, section 6](http://c-faq.com/aryptr/index.html). – Carl Norum May 07 '13 at 03:22
  • Sorry - that link isn't quite right. But if you search, you'll find many examples of identical questions. – Carl Norum May 07 '13 at 03:27
  • Thankfully there are people hired to weed out the potential *employees* who aren't prepared to research, read manuals, you know... Jobby stuff! If you want the job, you'll find the answer to that question and anticipate any other questions that might come up about the language by reading a book and meanwhile, thinking "Which question can I phrase from this sentence?". This advice might seem harsh, but it's realistic: Put yourself in the shoes of the interviewer, and learn the damned language *before* you get the interview, so that you don't waste any more of your time (or the time of others). – autistic May 07 '13 at 03:48
  • ... If you don't want to do any of that stuff, then you clearly don't want the job. Find something else to do. Again, it's harsh... but it's realistic. You won't be happy doing a job that you don't like, so find one that you *do* like and be a happy camper! If you *do* want the job, you should be able to show the interviewer that you *love* the work involved. Brag about your research skills, so they don't have to ask. – autistic May 07 '13 at 03:49
  • One additional note: `int *c=&a; // Yes, it works..`, but `int *c=&a[0]; // My strong preference whenever I need "addressof" an array`. IMHO... – paulsm4 May 07 '13 at 04:22
  • @paulsm4: what do you mean "addressof an array"? `&a` is the correct way to get a pointer to the array `a`. The OP is just not using it for that (he's assigning to an incompatible pointer type). – newacct May 07 '13 at 05:46

1 Answers1

0
int a[20];

You are declaring an array a which can hold 20 int

int *b=a;

Since a is an array, a evaluates to the base address of array a (i.e. first element of a). So b is an int pointer which points to the first element in array a

int *c=&a;

&a means address of a which is being assigned to int pointer c. But, this is not good as &a is address of an array (not the base address of the array), so you should define c as int ** (i.e. pointer to a pointer) or assign it to &a[0] (i.e. the address of the first element of array a). Since array is assigned contiguous memory, once you have the base address of the array, you can easily access the remaining elements of the array by doing *(b+i) where i is an index of the element you want to access.

However,

int a = 5;
int *b= a ; // WRONG -- b will not point to the address of variable a,  but to the memory location 5, which is what you do not want
int *c = &a; //CORRECT
Bill
  • 5,263
  • 6
  • 35
  • 50
  • Thanks bill, this I understand.I wanted to know here the difference between b=a and c=&a (ie. a and &a). – user2333014 May 07 '13 at 03:46
  • That is mentioned in the answer....`a` evaluates to the base address of the array (when declared as an array), but when it is a variable, it evaluates to the value of the variable, not the address. However, `&a` always evaluates to the address of `a`. Hope this clarifies it. – Bill May 07 '13 at 04:16
  • `b` points to the first element of `a`. `int *c=&a;` in OP's code is wrong because `&a` has type `int (*)[20]` and is a pointer to an array. Assigning it to a `int *` is assigning to an incompatible pointer type. – newacct May 07 '13 at 05:43