The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
41 views
#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??

asked in Programming by Veteran (111k points) | 41 views

1 Answer

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.

answered by Junior (501 points)
0
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
because one returns an unsigned value and other returns a signed value which differs in ranges and are printing different values.
0

@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
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.

Related questions

+1 vote
0 answers
6
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,540 questions
54,099 answers
187,269 comments
71,006 users