0

Say you have this C program

int main() { int *ptr; int ar[4]; ptr = ar; return 0; } 

would the address associated with the label ar hold the base address of the first element of the array or would it hold the first element itself? If its the second then when you have ptr = ar; ar must evaluate to the address its representing as opposed to what's in that address... right?

I would appreciate any input on this

1
  • Why not just test it yourself? Compare &ar[0] to (int *)ar. Commented Mar 9, 2012 at 18:08

4 Answers 4

3

ar is an array, which is distinct from a pointer.

However, in most situations (such as when you assign it to ptr), it decays to become a pointer, i.e. the address of the first element in the array.

Sign up to request clarification or add additional context in comments.

2 Comments

So do the array's values live on the stack like the other local variables to the method?
@user1259909: Well, that's up to the compiler. It's safer to say that local arrays act just like any other local variable.
0

It will hold the address. To access the first element, use arr[0] or ptr[0].

Comments

0

The value of an array name is the address of the first element of the array.

Comments

0

The expression ar always refers to an array object, and has the type int [4]. Except when ar is an operand of the sizeof or unary & operators, it will be replaced with ("decay to") an expression of type int * whose value is the address of the first element in the array.

So, given code like

printf("address of ar[0] is %p\n", (void *) ar); 

the first thing that happens is that the expression ar "decays" to a pointer expression whose type is int * and value is the same as &a[0], which is cast to void *, and that's what gets printed. The pointer value isn't stored at ar; it's computed from ar.

How this all gets translated to machine code is up to the compiler. Here's what gcc on linux does with it (I saved your code to a file named layout.c, and compiled it as gcc -o layout -ansi -pedantic -Wall -Wa,-aldh=layout.lst layout.c to get the following assembly listing):

GAS LISTING /tmp/fbgo448-tmp.359a6da/files/ccoNessg.s page 1 1 .file "layout.c" 2 .version "01.01" 3 gcc2_compiled.: 4 .text 5 .align 4 6 .globl main 8 main: 9 0000 55 pushl %ebp 10 0001 89E5 movl %esp, %ebp 11 0003 83EC28 subl $40, %esp 12 0006 8D45D8 leal -40(%ebp), %eax 13 0009 8945F4 movl %eax, -12(%ebp) 14 000c B8000000 movl $0, %eax 14 00 15 0011 C9 leave 16 0012 C3 ret 17 .Lfe1: 19 0013 90 .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.2 2.96-112.7.2)" 

The expression -40(%ebp) is the first element of ar; -12(%ebp) is ptr. Line 12 computes the effective address of the item at -40(%ebp) and saves that value to %eax, which is then written to -12(%ebp).

So in the context of gcc/linux, the answer to your question is that the address associated with ar contains the value of the first element. Note that the answer could be different on a different platform.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.