0

Possible Duplicate:
Is array name a pointer in C?
How do I use arrays in C++?

Following program shows the same attributes of an array and pointer... Are array elemenst somehow related to pinters?

#include <iostream>
using namespace std;
int main(){
    int a[3];
    for(int i=0;i<3;i++){
        cin>>*(a+i);
        cout<<*(a+1);
    }
    return 0;
} 
Community
  • 1
  • 1
sandbox
  • 2,631
  • 9
  • 33
  • 39
  • 8
    Simply put, [arrays are not pointers](http://stackoverflow.com/a/4810668/308661). However, arrays can be converted to pointers implicitly. – In silico Jan 19 '12 at 02:43

4 Answers4

4

the C FAQ answers a lot of the common questions about arrays and pointers, and that arrays aren't pointers but there is some magic going on that makes them look like pointers...

http://c-faq.com/aryptr/index.html

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

In C (and C++), you can add (or sub) index to array, and get the address of the member. You can also use the array as it was a pointer to its first member. See that code:

int a[5]={0,1,2,3,4};
int *p=a;
printf("%d %d\n",a[3],p[3]); //should print 3 3
printf("%p %p %p\n",p+3,a+3,&a[3]); //should print the same address 3 times.
asaelr
  • 5,438
  • 1
  • 16
  • 22
0

Pointer is a variable which contains the address of the element of the array. In this way it "points" to that array (to the element of array or to the beginning of array). What you use in this code is calculating the address of some element and then dereferencing that addresses. In fact, the code

*(a+i) // this is the value of the element with address a+i
       // here a - is a pointer to beginning of array "a"
       // a+i - is an address of particular element

is equivalent to

a[i]
user938720
  • 271
  • 3
  • 14
-4

Yes. Arrays and pointers are related. The array name, without the square bracket, ( a in your example) has the address of the base of the array or the 0th element of the array. So It is actually a pointer. Normal pointer addition works on it as well. Eg -

int arr[10];
cout<<"Address of the 0th element = " <<arr;
cout<<"Value of the 0th element = "<<*arr;      //arr is a pointer here
arr=arr+1;//arr now points to the 1st element of the array, 
Code maniac
  • 86
  • 1
  • 4
  • 3
    This is wrong. An array is not a pointer. An array name is not a pointer either. – wilhelmtell Jan 19 '12 at 02:52
  • This is incorrect. Name of an array *sometimes* acts as an pointer to its first element. – Alok Save Jan 19 '12 at 02:54
  • Any variable that contains an address is a pointer. arr has the address of the 0th element of the array. So logically arr is a pointer(not the array itself). I never said that an array is a pointer. – Code maniac Jan 19 '12 at 02:57
  • Is it correct to say that 1st element of an array is a Pointer? – sandbox Jan 19 '12 at 03:01
  • No. The 1st element of an array is not a pointer. Only the array name which contains the base address of the 0th element is a pointer – Code maniac Jan 19 '12 at 03:03
  • 2
    NO. Here is how it is - int a[5]; cout< – Code maniac Jan 19 '12 at 03:09
  • Wow!! great effort to make me understand. Thanks :) – sandbox Jan 19 '12 at 03:11
  • `Any variable that contains an address is a pointer.` Are you serious? – asaelr Jan 19 '12 at 03:15
  • 3
    "The array name [...] is actually a pointer." This is factually incorrect, so somewhere your logic is faulty. An 'array name', by the very two words, is the *name* of an *array*. Array's are not pointers. – GManNickG Jan 19 '12 at 03:19
  • 1
    `arr=arr+1`? Did you test that? – Benjamin Lindley Jan 19 '12 at 03:23
  • Why was this selected as the answer when it is so very wrong? – Benjamin Lindley Jan 19 '12 at 03:25
  • According to Wikipedia - "In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address." http://webcache.googleusercontent.com/search?q=cache:Jwg94oOq7gsJ:en.wikipedia.org/wiki/Pointer_%28computer_programming%29+&cd=5&hl=en&ct=clnk&gl=us&client=firefox-a . So any variable that has a memory address and points to another variable is a pointer. Please let me know what is wrong with this? What else should a variable containing an address be called ? – Code maniac Jan 19 '12 at 03:36
  • The variable does not contain the address. If you write the name of the variable, the compiler translate it to the address. and therefor `arr=arr+1;` is illegal. Trivia question: `void *p=&arr;int i=*(int*)p;void *q=*(void**)p;` what i and q contain? – asaelr Jan 19 '12 at 03:41
  • 2
    @PushkarGopalakrishna: An array does not contain an address. Neither does an array's name. An array's name is just how an array is referred to in source code. You are probably getting confused by the fact that when you try to print an array(via cout or printf) it gives you an address. This is because arrays are implicitly convertible to pointers, and the printing functions have no overloads that accept arrays. But they do have overloads that accept pointers, so the conversion is made. – Benjamin Lindley Jan 19 '12 at 03:44
  • hmmm . guess i was wrong :) sorry for the confusion folks and thanks for correcting it .. – Code maniac Jan 19 '12 at 03:54