473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Different value of same pointer variable.

#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
11 3080
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


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


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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
8
by: The Cool Giraffe | last post by:
I'm playing around with pointers trying to figure out how the operator & works. My impression was that it can be used to create a pointer to a thingy. So, i created the following code and ran it....
16
by: Virtual_X | last post by:
if we use char in a function parameter like that void st(char x) {cout << x;} and char* as a parameter in the same function instead of char x what would be different in my experiments i...
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.