xyz
xyz

Reputation: 8947

How does this C program compile and run successfully?

I was asked this in an interview. What will be the result of this program? I confidently said that this will not compile. I said that a is an array name and does not take up any space, so &a should not make any sense, hence this will not compile. but on hands-on, it turns out that compiles as well as runs successfully. Could someone please explain, how this works.

main()    
{
    int a[50];
    &a; // or perhaps the interviewer wanted to know the output of this line..
}

Upvotes: 3

Views: 721

Answers (5)

datenwolf
datenwolf

Reputation: 162327

The interviewer clearly should got new test material :) This…

main()    
{
    int a[50];
    &a;
}

… is old K&R C. If not declared otherwise a function returns int and the last statement of the function is returned implicitly. You can rewrite this to ANSI C the following:

int main()    
{
    int a[50];
    return &a;
}

So lets dissect this program:


int main()    

Define a function main returning an int, taking arbitrary parameters.


{
    int a[50];

Define an array of 50 int in automatic storage and assign an int pointer (int*) to the first element of this array to variable of the literal a.


    return (intptr_t)&a;
}

Now this is a bit tricky. Just writing a evaluates to the address of the first element of the array. The & operator takes the address of the variable this particular pointer is stored in. It doesn't matter that there are 50 int allocated somewhere and this variable currently points there. What we take is the pointer to the pointer, i.e. an int pointer pointer (int**).array, again its first element.

However K&R C defines implicit conversion of pointer types to integers. The value of this pointer uniquely identifies the original pointer and must be suited for pointer arithmetic. Most compilers implement this by simply storing the verbatim address of the pointer in the integer. Important exception: Any special machine dependent nil-pointer value must map to 0 on the C side and an int or intptr_t of 0 cast to a pointer must map to the architecture nil value (on most architectures the machine nil value is 0, too).

main returns the integer representation of the pointer, on most system its memory address, which by definition of the C standard designates it's exit code.


Compiling this program

cc -o interview_question interview_question.c

You can execute and display its exit code using

./interview_question ; echo $?

Which will be some arbitrary, but not a random number.

EDITs due to @John Bodes comment and corrections.

Upvotes: 3

RPS
RPS

Reputation: 141

This code should compile and run successfully as I feel. a is an array, &a is nothing but the address of first index (same as a). so &a is an expression which doesn't do anything ... its same as:

int x; &x;

There will not be any output of this as expression 2 is doing nothing.

Upvotes: 1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133122

This program has no observable result. It is compiled successfully but doesn't have any observable behavior.

&a is an expression. Because a is an object of array type, &a is the address of the array(its value may coincide with &a[0], but its type doesn't - int* vs int(*)[50]). Any expression followed by a semicolon is an expression statement. A function body is a sequence of zero or more statements enclosed in {} Therefore this is a legitimate main() function. (When you write a[0] = 10; this too is an expression statement.)

Under the as-if rule of the C++ standard, the implementation may even decide not to allocate any memory for your array, and do nothing at all, because even if it did, you wouldn't have any way to check it (at least from C++ point of view).

The following is also a valid C++ program

int main()
{
   1; 2; 3;
}

Upvotes: 3

Graham Borland
Graham Borland

Reputation: 60721

The &a; line just computes the address of the array a, and silently discards it.

Upvotes: 4

Karoly Horvath
Karoly Horvath

Reputation: 96326

you can use an expression as a statement, it's perfectly valid. It won't do anything.

Upvotes: 2

Related Questions