1

I tried some code to check the behavior of array and pointers. Its as follows.

#include <stdio.h>
main(){
int s[]={1,2};
int *b=s;
printf("%d, %d, %d\n", s, &s, *s);
printf("%d, %d, %d\n", b ,&b, *b);
}

Initially I thought of pointers and array to be same BUT...
To my surprise the value of 's' and '&s' are SAME unlike 'b'. Does that mean an Array variable "points to itself?"

I am also now confused with what actually is a variable "name"? How its binding takes place with a location in memory? I am just unable to visualize things that go on there! And where in memory (RAM) is all the information stored at run time?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Abhinav
  • 83
  • 7

4 Answers4

7

OK, let say the following is how the memory looks when you execute

int s[]={1,2};
int *b=s;

s[]
+-----+-----+
|  1  |  2  |
+-----+-----+
100   104 
^
|
|  int *b = &s
+-----+
| 100 |
+-----+
200

s is an array. What that means is, it is a contiguous memory location which is associated with a variable s and each element is accessed by offsetting the array variable name.

So when you use s it actually boils down to the address of the array (which is 100 in this case). And when you do *s, it boils down to *(s+0) which is equivalent of s[0] and so *s represents the contents stored in the zeroth location (in this case s[0] is 1). When do do an &s, this will print the address of the s (which is100` in this case).

Note that, here s and &s represents an address; *s and s[x] represents an integer.

The same applies to the pointer. So, b prints the content it has, which is the address of s (which is 100 in this case). &b prints the address of b, which is 200 in this case. And, *b prints the content of the first element of the array s which is 1.

I have modified you program to make it print the address.

#include <stdio.h>

int main(void)
{
    int s[]={1,2};
    int *b=s;
    printf("%p, %p, %d\n", (void *)s, (void *)&s, *s);
    printf("%p, %p, %d\n", (void *)b, (void *)&b, *b);
    return 0;
}

Output:

0xbfc4f3e4, 0xbfc4f3e4, 1
0xbfc4f3e4, 0xbfc4f3ec, 1

EDIT: %p expects void *. Added the same!

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
4

An array, when used as an argument to a function, decays into a pointer to its first element. Similarly, taking the address of an array results in a pointer to the location of the first element (but with a different type).

As for your second question, a variable name only exists at compile-time. It typically has no representation in memory at runtime.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be replaced with an expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.

Assume the following code:

int arr[10];
foo(arr);

In the call to the function foo, the expression arr is converted from type "10-element array of int" to "pointer to int", and the address of the first element of arr is what actually gets passed to foo.

We would define foo as either

void foo(int a[]) {}

or

void foo(int *a) {}

In the context of a function parameter declaration, T a[] and T a[N] are identical to T *a; the parameter is a pointer type, not an array type. Note that this is only true for function parameter declarations.

As mentioned above, one exception to this rule is when the array expression is the operand of the unary & operator. If we change the call to foo to read

foo(&arr);

then the type of the expression &arr is "pointer to 10-element array of int", or int (*)[10], and the value of the expression is the address of a. For this, the definition of foo would be

void foo(int (*a)[10]) {}

In C, the address of the array and the address of the first element of the array are the same - thus both of the expressions arr and &arr have the same value, but their types are different. This matters for operations involving pointer arithmetic. For example, assume our code had been written

int arr[10];
foo(arr);
...
void foo(int *a)
{
   ...
   a++;  
   ...
}

On entry, a points to arr[0]. The expression a++ would advance the pointer to point to the next integer in the array (arr[1]).

Now assume the code had been written as

int arr[10];
foo(&arr);
...
void foo(int (*a)[10])
{
  ...
  a++;
  ...
}

On entry, a still points to arr[0] (remember, the address of the array is the same as the address of the first element of the array), but this time the expression a++ will advance the pointer to point to the next 10-element array of integers; instead of advancing the pointer sizeof (int) bytes, we advance it sizeof (int[10]) bytes.

So this is why in your printf statement you see the same values for both s and &s. You should use the %p conversion specifier to print pointer values, and it expects the corresponding argument to be type void *, so change those printf statements to

printf("%p %p %d\n", (void *) s, (void *) &s, *s);
printf("%p %p %d\n", (void *) b, (void *) &b, *b);
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

A simple way to think about this is that an array as a pointer can't be changed by assignment, it is effectively a constant pointer to a known amount of memory.

To try that, use:

myptr = myarray;

Which is perfectly ok, and then try:

myarray = myptr;

Which is not.

Matt
  • 7,100
  • 3
  • 28
  • 58
  • 3
    It may be a simple way to think about it, but it's also a wrong way to think about it. An array is not a pointer, const or otherwise. Saying that it is will only lead to more questions later, at which time, someone will have to explain to him that an array is in fact *not* a pointer, and he won't know who to believe. – Benjamin Lindley Feb 03 '12 at 17:50