#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. 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-
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
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
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.
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.
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
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
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).
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
by: pinkfloydhomer |
last post by:
Is it well-defined and portable to do something like:
typedef struct
{
int type;
char c;
} S1;
typedef struct
{
|
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...
|
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....
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |