By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,910 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,910 IT Pros & Developers. It's quick & easy.

Different value of same pointer variable.

P: n/a
#include <stdio.h>

void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}

The result is, "400 1439 0 1439".

I don't understand "p" and "(int)p" show different result.

Please help me.

Jul 27 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
persevere wrote:
#include <stdio.h>

void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}

The result is, "400 1439 0 1439".

I don't understand "p" and "(int)p" show different result.

Please help me.

Maybe because this is not a C++ program? Try:

1) int main, instead of void main
2) reinterpret_cast<int>(p), instead of (int)p

Also, <iostream> is usually better than <stdio.h>

Hope this helps,
-shez-

Jul 27 '05 #2

P: n/a


persevere wrote:
#include <stdio.h>

void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}

The result is, "400 1439 0 1439".

I don't understand "p" and "(int)p" show different result.

Please help me.


you are using an uninitialized q when calculating p. Valid values for
pointers are 0, allocated memory, one past the allocated memory block.
Anything else leads to UB.

I compiled your code and I get warnings:
c:\MeasureString\copycon\copycon.cpp(9) : warning C4311: 'type cast' :
pointer truncation from 'int *' to 'int'
c:\MeasureString\copycon\copycon.cpp(9) : warning C4311: 'type cast' :
pointer truncation from 'int *' to 'int'
c:\MeasureString\copycon\copycon.cpp(9) : warning C4313: 'printf' :
'%d' in format string conflicts with argument 1 of type 'int *'
c:\MeasureString\copycon\copycon.cpp(9) : warning C4313: 'printf' :
'%d' in format string conflicts with argument 2 of type 'int *'
c:\measurestring\copycon\copycon.cpp(8) : warning C4700: local variable
'q' used without having been initialized

/dan

Jul 27 '05 #3

P: n/a
persevere wrote:

#include <stdio.h>

void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}

The result is, "400 1439 0 1439".

I don't understand "p" and "(int)p" show different result.
Because your program has lots of undefined behaviour.
1) main() returns int. Always
2) You start with a non initialized pointer
3) You do arithmetic to a pointer which do not point to the same object
4) You use the wrong formatting flag for outputting pointers

Please help me.


From the above 3, points 2 and 4 are serious enough that
anything can happen. Point 3 is a problem but can usually
be ignored on most systems (at least for an exampe like that)
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 27 '05 #4

P: n/a
After I change my code from C to C++, the results are also different.

I used

1. iostream.h,
2. int main (and "main" returns 0)
3. and "cout << p << " " << q << " " << (int)(p) << " " << (int)(q),

and the result is 0x4ce0191, 0x4ce0001, 401, 1.

I still don't understand.

Jul 27 '05 #5

P: n/a
My new source code is following.

#include <iostream.h>

int main ( void )
{
int x[200];
int *p, *q;

p = &x[0];
q = p + 200;
cout << p << " " << q << " " << (int)(p) << " " << (int)(q);

return 0;
}

The result is 0x8ffb0e68, 0x8ffb0ff8, 3688, 4088.

I start with a initialized pointer and "cout", but I don't understand
the result.

Jul 27 '05 #6

P: n/a


persevere wrote:
My new source code is following.

#include <iostream.h>

int main ( void )
{
int x[200];
int *p, *q;

p = &x[0];
q = p + 200;
cout << p << " " << q << " " << (int)(p) << " " << (int)(q);

return 0;
}

The result is 0x8ffb0e68, 0x8ffb0ff8, 3688, 4088.

I start with a initialized pointer and "cout", but I don't understand
the result.


4088 - 3688 = 400 which is 200 *2

200 as in q = p + 200
2 as in sizeof(int) == 2

learn your pointer arithmetic!

apparently the pointer size on your platform is 4 bytes and int size is
2. when you cast to int you truncate the value of the pointers:
0x8ffb0e68 -> 0x0e68 which is the decimal 3688
same goes for q.

HTH
/dan

Jul 27 '05 #7

P: n/a
persevere wrote:

My new source code is following.

#include <iostream.h>

int main ( void )
{
int x[200];
int *p, *q;

p = &x[0];
q = p + 200;
cout << p << " " << q << " " << (int)(p) << " " << (int)(q);

return 0;
}

The result is 0x8ffb0e68, 0x8ffb0ff8, 3688, 4088.

I start with a initialized pointer and "cout", but I don't understand
the result.


what is
sizeof( void* )
sizeof( int )
sizeof( long )
on your system?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 27 '05 #8

P: n/a
persevere wrote:

The result is 0x8ffb0e68, 0x8ffb0ff8, 3688, 4088.

I start with a initialized pointer and "cout", but I don't understand
the result.


Hint == sizeof(int).
Jul 27 '05 #9

P: n/a
persevere wrote:
My new source code is following.

#include <iostream.h>

int main ( void )
{
int x[200];
int *p, *q;

p = &x[0];
q = p + 200;
cout << p << " " << q << " " << (int)(p) << " " << (int)(q);

return 0;
}

The result is 0x8ffb0e68, 0x8ffb0ff8, 3688, 4088.

I start with a initialized pointer and "cout", but I don't understand
the result.


What is it you are trying to achieve with all this? Conversions from
pointers to integers is implementation-defined, and tells you nothing
about the language itself.

Learn to use pointers properly and stop this sort of aimless
exploration.


Brian
Jul 27 '05 #10

P: n/a
Shezan Baig wrote:
Maybe because this is not a C++ program? Try:
2) reinterpret_cast<int>(p), instead of (int)p

THe above is perfectly fine C++. (int) p when p is of type
int* means exactly the samething.

The big issue is that passing pointers to the "%d" arguments
of printf is ALSO wrong.

Further expecting anything useful from a reinterpret cast of
an int to a pointer is wishful thinking.
Jul 28 '05 #11

P: n/a
persevere wrote:
and the result is 0x4ce0191, 0x4ce0001, 401, 1.

I still don't understand.


Nobody said that reinterpret_casting a pointer to int was
going to be meaningful.
Jul 28 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.