1

I was experimenting with array of pointers address calculation arithmetic.I got confused with the output of the above code.Could anyone explain whats happening?

void foo()
{
    int i=10,k=3,l=20,m=30;
    int *ary[2];
    ary[0]=&i;
    int b=20;
    ary[1]=&k;

    printf("%d\n",ary[0][1]);
}

Output is 3

Second program

void foo()
{
    int i=10,k=3,l=20,m=30;
    int *ary[2];
    ary[0]=&i;
    int b=20;
    ary[1]=&b;

    printf("%d\n",ary[0][1]);
}

Output is 20.

How is address calculation done in these above codes?

11
  • 1
    What's the difference between those 2 programs? Commented Jun 24, 2012 at 6:44
  • What @Jon Lin said, and how are you calling foo? There's a chance for undefined behavior... Commented Jun 24, 2012 at 6:44
  • The difference is that ari[1] is assigned &k in the first program and &b in the second. Commented Jun 24, 2012 at 6:49
  • sorry....changed the code now...now it has no undefined behaviour.. Commented Jun 24, 2012 at 6:50
  • Both programs print 10 on my mac. Commented Jun 24, 2012 at 6:54

2 Answers 2

3

In both code samples ary[0] is a pointer to a single integer. So when you do array[0][1] you're accessing that pointer out of bounds. So the behavior of both of your code samples is undefined.

The reason that you're getting the behavior you're seeing on your particular compiler is probably, that all the variables whose address you don't take are stored in registers and not in memory (or possibly they aren't stored at all because you never use them).

So in example 1 the only variables in memory are i and k. And in example 2 the only variables in memory are i and b. So in example 1 k is the variables that comes directly after i in memory and example 2 that variable is b.

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

Comments

0

Your argument is

int *ary[]

and in function arguments, array decay to pointers. So you basically have

int** ary

You then set the pointer at index 0, presumably a valid index, to the value of a single variable.

ary[0]=&i;

Then your code prints

ary[0][1]

which is basically

( &i )[1]

triggering undefined behavior, hence whatever result you get is a valid result.

2 Comments

my speculation was about how compiler does this.shouldnt it give value of variable immediately next to it.why does it gives value of k first time and then value of b next.shouldnt it give same variable each time?
@vindhya: When you trigger undefined behavior, the compiler has free pass to do whatever it wants to do. It could just crash, it could just return some random value, it could just return the variable immediately next to it... It could even decide that under no defined behavior will you call foo, and hence generate no code for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.