Can you please tell me how is the processing of this code taking place.
Because its giving a fixed address same as mentioned in the book.
The Answer to this question is

Address of i=FFE4
value at ptr =10

Address of j=FFE2
value at prt=20

how come addresses are these, and with just a difference of 2. And why these address are fixed.

Thanks in advance

#include<stdio.h>
#include<conio.h>

void main()
{
	int i=10,j=20;
	const int *ptr=&i;
	clrscr();
	printf("Address of i=%5x \n",ptr);
	printf("value at ptr=%d \n",*ptr);

	ptr=&j;

	printf("Address of j=%5x \n",ptr);
	printf("value at ptr=%d \n",*ptr);

	getch();

}

Recommended Answers

All 2 Replies

And why these address are fixed.

The addresses aren't fixed, but the likelihood of the addresses being right next to each other is pretty good. Here's my output (after fixing your Turbo C crap boilerplate):

Address of i=28ff18
value at ptr=10
Address of j=28ff14
value at ptr=20

Notice that the difference between the address of i and the address of j is four bytes, which is understandable because my implementation uses 32-bit integers. Your implementation uses 16-bit integers because it's as old as dirt, hence the two byte difference.

Note that this has nothing to do with the const keyword, it's completely how the compiler chooses to allocate memory for local variables.

Thanks Narue ........got it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.