in Programming in C
436 views
0 votes
0 votes
#include <stdio.h>

int main(void) {
    int s[6]={128,256,512,1024,2048,4096};
    int *x=(int*)(&s+1);
    printf("%d",x);
    return 0;
}
#include <stdio.h>

int main(void) {
    int s[6]={128,256,512,1024,2048,4096};
    int *x=(int*)(&s+1);
    printf("%u",x);
    return 0;
}

Why both are printing different address? Is it not a fixed address of memory??

in Programming in C
by
436 views

1 Answer

0 votes
0 votes

Though the array s is of fixed size i.e, 6.

x is a pointer which is pointed to array's first element.

Ans also, we are printing the value stored in x i.e, address to which it is pointed to but not the value stored at that address.

When we run these programs, the array will be allocated in memory. and x prints the address. so each time you run it, it will print different address. %d and %u both prints the address.

4 Comments

but why different addresses?? that is my question.

Why not they print same addresses, as both of the pointer points to the end of array and that is a fixed location

right??
0
0
because one returns an unsigned value and other returns a signed value which differs in ranges and are printing different values.
2
2

@Sathuri Bharath

Signed and unsigned address range is different, but when they are pointing , say if 1000 is starting address and array ends at 1000+4*6=1024

So, their both should print 1024 

right?

Why different address??

0
0
mam, it differs when %d prints negative and %u prints positive values. I have tried it on online and offline compilers and have printed the values within the same program. sometimes, the values are same and sometimes differ. it differs when one is negative and other is positive and some times two different positive values. I am not sure what is the exact reason for this.
0
0

Related questions