473,406 Members | 2,843 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,406 software developers and data experts.

pointer to string and then back to pointer on a 64 bit machine.

How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.

printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?

Sep 27 '07 #1
17 2118
atol does not work !
It does not bring back the correct value from the string buffer.
sscanf with %lu does it very well.
#include <stdio.h>

int main () {
char *p = (char *)182947876880;
char *p2;
char buf[100];

printf ("\n\n");
printf ("lu = %lu\n", p);

sprintf (buf, "%lu", p);
printf ("buf = %s\n", buf);

p2 = (char *)atol (buf); // THIS LINE DOES NOT WORK
sscanf (buf, "%lu", &p2); // sscanf DOES THE EXPECTED JOB
printf ("lu = %lu\n", p2);

if (p==p2) {
printf ("equal\n");
} else {
printf ("NOT equal\n");
}

printf ("\n\n");
}

Sep 27 '07 #2
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.

printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?
That's because %d isn't for pointers, %p is.

--
Ian Collins.
Sep 27 '07 #3
Is there a corresponding %p in sscanf ?

Sep 27 '07 #4
yes there is...
I replaced %lu with %p in the above program and it worked !

thanks for the help !

Sep 27 '07 #5
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.

printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?
Your references to a "64 bit machine" suggest that you don't really
understand pointers. They are to an address space for which the values
may not correspond to any physical address on your machine.

And "%d" is a specifier for signed ints, not for pointers. Check the
code below for hints about how to do what you _may_ (or may not) be
meaning to do. Your question is underspecified, as I'm sure you will
realize on reflection.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *buffer;
size_t n;

/* find out what the length of the string needs to be */
n = snprintf(0, 0, "%p", (void *) &buffer);

/* allocate the space */
if (!(buffer = malloc(n + 1))) {
fprintf(stderr, "could not allocate space for buffer.\n"
"giving up ...\n");
exit(EXIT_FAILURE);
}

/* put the address of buffer into buffer and show it */
sprintf(buffer, "%p", (void *) buffer);
printf("The buffer is at %p, and contains the string \"%s\"\n",
(void *) buffer, buffer);

free(buffer);
return 0;
}

The buffer is at 20d98, and contains the string "20d98"
Sep 27 '07 #6
Martin Ambuhl <ma*****@earthlink.netwrites:
let_the_best_man_win wrote:
>How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.

printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?

Your references to a "64 bit machine" suggest that you don't really
understand pointers. They are to an address space for which the
How do you figure that out? He might have got the wrong specifier for a
pointer but that was about it. A 64 bit machine running a program
compiled for 64 bit will almost certainly have 64 bit pointers. Or?

Sep 27 '07 #7
let_the_best_man_win wrote:
atol does not work !
It does not bring back the correct value from the string buffer.
sscanf with %lu does it very well.
atol is to convert strings to longs, which are integers.
Pointers are not long ints, or any kind of integer.
Using "%lu" is an error as well.
>

#include <stdio.h>

int main () {
char *p = (char *)182947876880;
char *p2;
char buf[100];

printf ("\n\n");
printf ("lu = %lu\n", p);

sprintf (buf, "%lu", p);
printf ("buf = %s\n", buf);

p2 = (char *)atol (buf); // THIS LINE DOES NOT WORK
sscanf (buf, "%lu", &p2); // sscanf DOES THE EXPECTED JOB
printf ("lu = %lu\n", p2);

if (p==p2) {
printf ("equal\n");
} else {
printf ("NOT equal\n");
}

printf ("\n\n");
}
Sep 27 '07 #8
On Sep 27, 7:43 am, Richard <rgr...@gmail.comwrote:
Martin Ambuhl <mamb...@earthlink.netwrites:
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.
printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?
Your references to a "64 bit machine" suggest that you don't really
understand pointers. They are to an address space for which the

How do you figure that out? He might have got the wrong specifier for a
pointer but that was about it. A 64 bit machine running a program
compiled for 64 bit will almost certainly have 64 bit pointers. Or?
I suppose so, but...

1) the OP didn't make it clear that the machine from which he will
print the pointer is a 64bit machine.

2) Unless the pointer is being printed and the printed value read in
the same invocation of the same program (that is, using a printed
value as a temporary storage for a pointer), then the thing pointed to
by the pointer is probably /not/ available to the program reading the
pointer. Hopefully, the program reading the printed pointer value
doesn't want the data pointed to by the pointer.

Sep 27 '07 #9
On Thu, 27 Sep 2007 05:06:06 -0700,
Lew Pitcher <lp******@teksavvy.comwrote:
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
[snip intermediate discussion]
1) the OP didn't make it clear that the machine from which he will
print the pointer is a 64bit machine.
He probably did intend it though. His original question is maybe a bit
ambiguous; 'on a 64 bit machine' can, if you want, just refer to 'read
it back', but it can also refer to the the whole previous sentence,
which, I think, is more likely.

Especially, since later in the OP we find:
>printing with %d does not capture the full 64-bits of the pointer
which, to me at least, resolves the ambiguity. I fail to see why the
poster would say this if they were printing the pointer on a machine
with a different configuration.

Also note that the OP did not say that he wanted to print to a file,
socket, or other external medium which then could be read somewhere
else, or even by another invocation of the same program. The poster
states that they want to print to a string buffer. Generally a string
buffer on a 32 bit machine is not available to read on a, different, 64
bit machine without some intermediate representation.

Martien
--
|
Martien Verbruggen | Make it idiot proof and someone will make a
| better idiot.
|
Sep 27 '07 #10
let_the_best_man_win wrote:
>
How do I print a pointer address into a string buffer and then
read it back on a 64 bit machine ?
Use %p. Make sure the pointer is cast to void* for printing.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 27 '07 #11
On Sep 27, 7:42 pm, CBFalconer <cbfalco...@yahoo.comwrote:
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then
read it back on a 64 bit machine ?

Use %p. Make sure the pointer is cast to void* for printing.
Why this restriction of casting to a void* ?
Shouldn't the size of all pointers be same ?
(Now please do not go on to HUGE and LONG pointers, whatever they
are !)
(I just mean simple pointers)

Sep 27 '07 #12
let_the_best_man_win wrote:
>
On Sep 27, 7:42 pm, CBFalconer <cbfalco...@yahoo.comwrote:
let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then
read it back on a 64 bit machine ?
Use %p. Make sure the pointer is cast to void* for printing.

Why this restriction of casting to a void* ?
Shouldn't the size of all pointers be same ?
No. All pointers can be cast to void* and back to the same type.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 27 '07 #13

"let_the_best_man_win" <sg********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
Is there a corresponding %p in sscanf ?
hopefully you are not storing pointers in files...

just be aware, a pointer is (in general) only valid within a particular
instance of an app.
as a result, saving pointers into a file, or transferring them between
instances, is a bad idea (in the next run, or on the other other box, or
just in another process, the pointer can point to something completely
different).

within the same instance of the same app it is ok though, or (for most
things) between different threads in the same instance.

or such...
Sep 27 '07 #14
CBFalconer wrote:

[...]
No. All pointers can be cast to void* and back to the same type.
Nope. All pointers to objects can, but there is no such guarantee for
function pointers.

--
Tor <torust [at] online [dot] no>

"Premature tuning is the root of all evil"
Sep 27 '07 #15
Tor Rustad wrote:
CBFalconer wrote:

[...]
>No. All pointers can be cast to void* and back to the same type.

Nope. All pointers to objects can, but there is no such guarantee
for function pointers.
True. I know that, but never seem to remember to mention it.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 27 '07 #16
On Thu, 27 Sep 2007 15:32:05 -0000, let_the_best_man_win
<sg********@gmail.comwrote:
>On Sep 27, 7:42 pm, CBFalconer <cbfalco...@yahoo.comwrote:
>let_the_best_man_win wrote:
How do I print a pointer address into a string buffer and then
read it back on a 64 bit machine ?

Use %p. Make sure the pointer is cast to void* for printing.

Why this restriction of casting to a void* ?
Because the answer to the next question is no.
>Shouldn't the size of all pointers be same ?
The only requirement is that void* and char* have the same size,
alignment, and representation. Other types of pointers are not
constrained this way.
>(Now please do not go on to HUGE and LONG pointers, whatever they
are !)
There are no HUGE or LONG pointers in C. There are pointers to long
but C is case sensitive. And a long* need not be the same size as any
other pointer.
Remove del for email
Sep 28 '07 #17
"Martin Ambuhl" <ma*****@earthlink.neta écrit dans le message de news:
5m************@mid.individual.net...
let_the_best_man_win wrote:
>How do I print a pointer address into a string buffer and then read it
back on a 64 bit machine ?
Compulsion is to use string (char * buffer) only.

printing with %d does not capture the full 64-bits of the pointer.
does %l exist in both printf and scanf for this purpose ?

Your references to a "64 bit machine" suggest that you don't really
understand pointers. They are to an address space for which the values
may not correspond to any physical address on your machine.

And "%d" is a specifier for signed ints, not for pointers. Check the code
below for hints about how to do what you _may_ (or may not) be meaning to
do. Your question is underspecified, as I'm sure you will realize on
reflection.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *buffer;
size_t n;

/* find out what the length of the string needs to be */
n = snprintf(0, 0, "%p", (void *) &buffer);
You compute the length of the representation of &buffer, which is the
address of the pointer, not the pointer itself.
/* allocate the space */
if (!(buffer = malloc(n + 1))) {
fprintf(stderr, "could not allocate space for buffer.\n"
"giving up ...\n");
exit(EXIT_FAILURE);
}
good, now buffer has a value, but there is no guarantee it is large enough
for the representation of its own address by %p.
/* put the address of buffer into buffer and show it */
sprintf(buffer, "%p", (void *) buffer);
bingo! potential buffer overflow
printf("The buffer is at %p, and contains the string \"%s\"\n",
(void *) buffer, buffer);

free(buffer);
return 0;
}

The buffer is at 20d98, and contains the string "20d98"
The above code is broken.

--
Chqrlie.
Sep 29 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ellarco | last post by:
``Opaque-pointer representing the ID of an object. struct _objID; typedef struct _objID * objectID;'' Hi again. Im using an api that defines an objectID type. The above represents the extent...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
12
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is,...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.