如何在char *中存储指针地址 [英] how to store pointer adress in a char*

查看:162
本文介绍了如何在char *中存储指针地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i之前遇到过这个问题(发布在这里然后解决了)现在我有了同样的问题但是更加复杂和一般的...... b / br ... >

基本上我想将一个istream的地址存储在一个字符*中,然后在其他信息中查找其他信息并稍后检索...就像

这个:

***********************


// supossing我有一个istream

istream in(" test.txt");

//我将它的地址存储为char

char * add =(char *)& in;

// [....]


//在我创建的另一个函数的代码中指针

istream * t;

//并恢复其地址

t =(ifstream *)add;


*******************


现在可行了......

我现在想要的不仅是存储地址而且还存储一些文字...

并且整个应该看起来像文件名

让我们说地址是0xDEAD我我想要的e char *看起来像这样


" file:DEADtext2.txt"


前缀有固定长度以及后缀....

i认为地址是32位(4字节32位奔腾机器)


所以我想首先存储在这样的一些字符串中(抱歉丑陋的

代码,但我绝望:)):

********

//商店前缀

string pref =" http:":


//存储地址

string add =(char *)& in :


//商店后缀

string suf =" text2.txt":


//连接所有三个

字符串总数= pref + add + suf;


char * add = total.cstr();

* ******

cout<< sizeof(& in)<< endl;

表示istream的大小是4个字节...


但是当我显示add时。 length()它说它只有三个字节长...所以

我很困惑...所以最后一个字节在哪里?


当我显示istream的元素:

cout<<(int)((char *)& in)[0]<< endl;

cout<<(int)((char *)& in)[1]<< endl;

cout<<(int)((char *)& in )[2]<< endl;

cout<<(int)((char *)& in)[3]<< endl;

然后元素3是零...因此只有三个字节...


所以两个请求帮助:


地址实际上有多长?


我怎样才能连接这三条信息并稍后检索它们?b $ b它们以后呢?


提前感谢所有想法,提示在哪里进一步寻找和

答案......

解决方案

如果你得到任何地址,它不是文本字符串。所以它可能包含在任何

字节0(零)中。如果你想获得指针的文本表示(我想看某些跟踪,我认为没有其他有用的用法),用这种方式写入

string:


#include< sstream>


std :: ostringstream osstr;

osstr<< &安培;中; //& in是必需的指针

std :: string value = osstr.c_str();


mw **** @ freenet.de 写道:




i之前遇到过这个问题(发布在这里然后解决了)现在我有了同样的问题但是更加复杂和一般......


基本上我想将一个istream的地址存储在一个char *中,然后在其他信息信息中找到其他信息并稍后检索...



为什么?


是这样的:

****************** *****


// supossing我有一个istream

istream in(" test.txt");


//我将其地址存储为字符

char * add =(char *)& in;



我宁愿使用C ++演员。


// [。 ...]


//在另一个函数的代码里面我创建一个指针

istream * t;


//并恢复其地址

t =(ifstream *)add;


************** *****


现在可以正常使用...


我现在想要的不仅是存储地址而且还存储一些文本...

,整个应该看起来像一个文件名

让我们说地址是0xDEAD我希望char *看起来像这样


" file:DEADtext2.txt"


前缀有固定长度以及后缀....

i thought地址是32位(4字节32位奔腾机器)



为什么不把你的所有数据都放到一个结构中并得到指向
那个?


所以我想先存储在这样的一些字符串中(抱歉丑陋的

代码,但我绝望:)):

********

//商店前缀

string pref =" http:":



您的意思是:


string pref =" http:" ;;


//存储地址

string add =(char *)& ;在:



您的意思是:


string add =(char *)& in;

这一行将尝试将& in解释为指向

空终止数组char(也称为C样式字符串)的第一个元素并复制到

表示要添加的数组。肯定不是你想要的。


//商店后缀

string suf =" text2.txt":



您的意思是:


string suf =" text2.txt" ;;


//连接所有三个

string total = pref + add + suf;


char * add = total.cstr();



add已经被定义为字符串,所以你应该在这里得到一个错误。


*** ****

cout<< sizeof(& in)<< endl;

表示istream的大小是4个字节...



No.它表示指向istream的指针的大小是4个字节。


但是当我显示add.length()时它表示它只有三个字节长......所以

我很困惑......所以最后一个字节在哪里?



添加不会保持指向istream的指针。它将尝试将

流解释为C样式字符串(它不是)。不要这样做!


当我显示istream的元素时:


cout<<( int)((char *)& in)[0]<< endl;

cout<<(int)((char *)& in)[1]<< ; endl;

cout<<(int)((char *)& in)[2]<< endl;

cout<<(int )((char *)& in)[3]<< endl;


然后元素3为零...因此只有三个字节...



可能有,或者可能有更多。流对象不是C风格的

字符串。它可能在任何地方都包含零字节。


所以请求帮助:


地址实际上有多长?



地址是4个字节,但那是相关的。


我怎样才能连接那些三条信息和检索

以后它们也是?



最好不要这样做。你想要实现什么?实际上是什么?


最好不要这样做。你想要实现


实际上是什么?





感谢您的回复(并抱歉:而不是;字体是

太小了:))


情况如下:


i在c ++(A)中有一个prrogram,在C中有一个库(B )我无法改变。

但是这个库可以通过插件扩展。我正在用C ++(C)编写

插件。


现在我在A中的ifstream中打开一个文件但是B不能处理ifstreams和

它只接受文件名...所以我想用插件C打开并阅读

然后将它传回给B.


1.-(A)打开文件并创建一个指针,将指针隐藏在

文件名中并传递给(B)

2.-(B)收到filename(char *)并传递给插件(C)

3.-(C)读取文件并将内容传递给(B)

所有其他工作是完成(插件,程序,开放等)。

剩下的唯一问题是如何传递包含指针的char *

从(A)到(C)


i使用结构没有区别,因为char *需要

看起来像一个文件名(解析后找到''文件:' '和文件

扩展名传递给我的插件!)

i希望我的问题现在更清楚了。

<登记/>

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
other pieces of information information and retrieve it later... like
this:
***********************

//supossing i have a istream
istream in("test.txt");
//i store its adress as a char
char* add=(char*)&in;
//[....]

//inside the code of another function i create a pointer
istream * t;
//and restore its adress back
t=(ifstream*)add;

*******************

now thats works ok...
what i want now is to store not only the adress but also some text...
and the whole should look like a filename
lets say the adress is 0xDEAD i would like the char* to look like this

"file:DEADtext2.txt"

the prefix has a fixed length as well as the suffix....
i thought the adress would be 32 bit (4 bytes 32bit pentium machine)

so i wanted to store first in some strings like this(sorry for the ugly
code but im desperate :) ):
********
//store prefix
string pref="http:":

//store adress
string add=(char*)&in:

//store suffix
string suf="text2.txt":

//concatenate all three
string total = pref + add + suf;

char* add= total.cstr();
*******
cout<< sizeof(&in)<<endl;
says the size of the istream is 4 bytes...

but when i display add.length() it says its only three bytes long... so
im quite confused... so where is the last byte?

also when i display the elements of the istream:

cout<<(int)((char*)&in)[0]<<endl;
cout<<(int)((char*)&in)[1]<<endl;
cout<<(int)((char*)&in)[2]<<endl;
cout<<(int)((char*)&in)[3]<<endl;

then element 3 is a zero... thus there are only three bytes...

so two pleas for help:

how long is the adress actually?

how can i concatenate those three pieces of information and retrieve
them later too??

thanks in advance to all ideas, hints where to further look for and
answers...

解决方案

If you get any address, it is not text string. So it may contain in any
byte 0 (zero). If you want to get text representation of pointer (I
think for some tracing, I see no other usefull usage), write it to
string this way:

#include <sstream>

std::ostringstream osstr;
osstr << &in; // &in is required pointer
std::string value = osstr.c_str();


mw****@freenet.de wrote:

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
other pieces of information information and retrieve it later...

Why?

like this:
***********************

//supossing i have a istream
istream in("test.txt");
//i store its adress as a char
char* add=(char*)&in;

I''d rather use a C++ cast.

//[....]

//inside the code of another function i create a pointer
istream * t;
//and restore its adress back
t=(ifstream*)add;

*******************

now thats works ok...
what i want now is to store not only the adress but also some text...
and the whole should look like a filename
lets say the adress is 0xDEAD i would like the char* to look like this

"file:DEADtext2.txt"

the prefix has a fixed length as well as the suffix....
i thought the adress would be 32 bit (4 bytes 32bit pentium machine)

Why don''t you just put all your data into a struct and get a pointer to
that?

so i wanted to store first in some strings like this(sorry for the ugly
code but im desperate :) ):
********
//store prefix
string pref="http:":


Did you mean:

string pref="http:";

//store adress
string add=(char*)&in:

Did you mean:

string add=(char*)&in;
This line will try to interpret &in as a pointer to the first element of a
null terminated array of char (also known as C style string) and copy over
that array into add. Most certainly not what you want.

//store suffix
string suf="text2.txt":

Did you mean:

string suf="text2.txt";

//concatenate all three
string total = pref + add + suf;

char* add= total.cstr();

add is already defined as string, so you should get an error here.

*******
cout<< sizeof(&in)<<endl;
says the size of the istream is 4 bytes...

No. It says that the size of a pointer to istream is 4 bytes.

but when i display add.length() it says its only three bytes long... so
im quite confused... so where is the last byte?

add won''t hold the pointer to the istream. It will try to interpret the
stream as a C style string (which it isn''t). Don''t do that!

also when i display the elements of the istream:

cout<<(int)((char*)&in)[0]<<endl;
cout<<(int)((char*)&in)[1]<<endl;
cout<<(int)((char*)&in)[2]<<endl;
cout<<(int)((char*)&in)[3]<<endl;

then element 3 is a zero... thus there are only three bytes...

There might be, or there might be more. A stream object is not a C style
string. It may contain zero bytes wherever it wants.

so two pleas for help:

how long is the adress actually?

The address is 4 bytes, but that''s irelevant.

how can i concatenate those three pieces of information and retrieve
them later too??

It wold be best not to do that at all. What are you trying to achieve
actually?


It wold be best not to do that at all. What are you trying to achieve

actually?

hi,
thanks for the responses(and sorry for the : instead of ; the font was
too litle :))

The situation is as follows:

i have a prrogram in c++(A) and a library in C (B) which i cant change.
However this library can be extended by plugins. I am writing the
plugin in C++(C).

Now i open a file in a ifstream in A but B cant process ifstreams and
it accepts filenames only... so i want to use plugin C to open and read
it and pass it back to B.

1.- (A) opens the file and creates a pointer hides the pointer in a
filename and pass to (B)
2.- (B) receives the filename (char*) and passes to plugin (C)
3.- (C) reads the file and passes the content to (B )
All the other work is done (plugin, program, opening etc..)
the only problem left is how to pass the char* containing the pointer
from (A) to (C)

i there would be no difference to using a struct as the char* needs to
look like a filename (its parsed and upon finding the ''file:'' and file
extension passed to my plugin!)

i hope my problem is clearer now.


这篇关于如何在char *中存储指针地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆