为什么address-of-char会给我带来非终止的c-string乱码? [英] Why does address-of-char give me non-terminated c-string gibberish?

查看:87
本文介绍了为什么address-of-char会给我带来非终止的c-string乱码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这不是最小的代码,但至少它给出了相当不错的想法

- 是的,我是' ma newb :(


我给出这个代码的意思是所有三个变量看起来都是我的眼睛

好​​像他们正在接受以同样的方式,所以我结束了对于为什么char的地址没有被返回的方式感到困惑

其他类型的地址做。


(对不起格式错误 - 我直接从我的IDE中复制/粘贴......)


Lil洞察力?


谢谢!


==============

#include< iostream> ;


使用命名空间std;


void main()

{

int a = 1;

char b =''q'';

double c = 1.3456;


cout< <" Type Size Value Address"<< endl;


cout<<" int"<<"" << sizeof(a)<< " " << a<<

<< & a<< endl;

cout<< "炭" << " " << sizeof(b)<< " " << b<<

<< & b<< endl;


//为什么上面的行给char的地址乱码?


//它就像虽然这与c风格字符串的问题有关,但是最后是一个char + a null

//的数组,如果你忘记了在制作字符串时将空值设为

手动,并且

//尝试将其打印出来,最终每个内存位置都是
$打印b $ b

//碰巧遇到空值。但我真的不知道

这里的交易是什么。


//请注意,如果我使用以下内容开始,我会得到预期的结果/>
with,并在以后更改显示

//更改:

// char * b = new char(''q'') ; (等......)


cout<< "双" << " " << sizeof(c)<< " " << c<< "

<< & c<<结束;


返回;

}

Hi all,

This isn''t minimal code, but at least it gives the idea reasonably well
- and yes, I''m a newb :(

The point of me giving this code is that all three vars look to my eye
as though they''re being treated in exactly the same way, so that I end
up confused about why the address-of-char doesn''t get returned the way
the other address-of-types do.

(Sorry for bad formatting - I copy/pasted straight out of my IDE...)

Lil insight?

Thanks!

==============
#include <iostream>

using namespace std;

void main()
{
int a = 1;
char b = ''q'';
double c = 1.3456;

cout << "Type Size Value Address" << endl;

cout << "int" << " " << sizeof(a) << " " << a << "
" << &a << endl;
cout << "char" << " " << sizeof(b) << " " << b << "
" << &b << endl;

//Why does the above line give gibberish for the address-of-char?

//It''s as though it''s related to the issue of a c-style string being
an array of char + a null
//on the end, and if you forget to put the null when making a string
"manually", and
//try to print it out, you end up with every memory location being
printed until it
//happens to run across a null. But I really have no idea what the
deal here is.

//Note that I get the expected result if I use the following to begin
with, and put in the obvious
//changes later on:
//char* b = new char(''q''); (etc...)

cout << "double" << " " << sizeof(c) << " " << c << " "
<< &c << endl;

return;
}

推荐答案

在2005-10-25, sh ************* @gmail .com < sh ************* @ gmail.com>写道:
On 2005-10-25, sh*************@gmail.com <sh*************@gmail.com> wrote:
大家好,

这不是最小的代码,但至少它给出的想法相当不错 - 是的,我是newb :(

我给出这个代码的重点是,所有三个变量看起来都像我的眼睛一样,好像他们正在以完全相同的方式对待,
所以我最终混淆了为什么char的地址没有按照其他地址类型的方式返回。

(抱歉格式错误 - 我复制/直接从我的
IDE粘贴...)


检查您的IDE是否有将标签转换为空格的选项。

将发布代码时纠正任何问题。


如果没有,请考虑编写自己的转换器,作为练习。#include< iostream>

使用命名空间std ;

void main()


main返回一个int。

{
int a = 1;
char b =''q'';
double c = 1.3456;

cout<< 类型大小值地址 << endl;

cout<< " INT" << " " << sizeof(a)<< " " << a<<
<< & a<< endl;
cout<< "炭" << " " << sizeof(b)<< " " << b<<
<< & b<< endl;

//为什么上面的行给char的地址乱码?
//好像它与c的问题有关-style string
char + a null


那是对的。标准的ostream认为指向char的指针是一个

的C字符串。将指针转换为void *以输出它们。


cout<< " INT" << " " << sizeof(a)<< "

<< a<< " " << static_cast< void *>(& a)<< endl;

cout<< "炭" << " " << sizeof(b)<< "

<< b<< " " << static_cast< void *>(& b)<<结束;


顺便说一下,你应该看看io操纵器。他们可能会使你的小桌子更简单。

返回;


返回0;

}
Hi all,

This isn''t minimal code, but at least it gives the idea
reasonably well - and yes, I''m a newb :(

The point of me giving this code is that all three vars look to
my eye as though they''re being treated in exactly the same way,
so that I end up confused about why the address-of-char doesn''t
get returned the way the other address-of-types do.

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)
Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.

If not, consider writing your own converter, as an exercise. #include <iostream>

using namespace std;

void main()
main returns an int.
{
int a = 1;
char b = ''q'';
double c = 1.3456;

cout << "Type Size Value Address" << endl;

cout << "int" << " " << sizeof(a) << " " << a << "
" << &a << endl;
cout << "char" << " " << sizeof(b) << " " << b << "
" << &b << endl;

//Why does the above line give gibberish for the address-of-char?
//It''s as though it''s related to the issue of a c-style string being
an array of char + a null
That''s right. The standard ostream thinks a pointer to char is a
C-string. Cast your pointers to void* to output them.

cout << "int" << " " << sizeof(a) << " "
<< a << " " << static_cast<void*>(&a) << endl;
cout << "char" << " " << sizeof(b) << " "
<< b << " " << static_cast<void*>(&b) << endl;

As an aside, you should look into the io manipulators. They may make
building your little table simpler.
return;
return 0;
}




-

Neil Cerutti



--
Neil Cerutti


" Neil Cerutti" <乐******* @ email.com>在留言中写道

news:sl ********************** @ FIAD06.norwich.edu
"Neil Cerutti" <le*******@email.com> wrote in message
news:sl**********************@FIAD06.norwich.edu
在2005-10-25, sh ************* @gmail .com < sh ************* @ gmail.com>
写道:
On 2005-10-25, sh*************@gmail.com <sh*************@gmail.com>
wrote:

(对不起格式错误 - 我直接从我的
IDE中复制/粘贴...)

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)



检查您的IDE是否有将标签转换为空格的选项。那个
会在发布代码时纠正任何问题。



Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.




你会这样想的不是你。实际上,如果将
直接从VC ++ IDE粘贴到Outlook Express中则不然。请注意下面的空格

only代码出来了(main中的两行都是在

VC ++ IDE中缩进,有4个空格):


int main()

{

int x = 1;

返回0;

}


如果你遵循一个更间接的路线:VC ++到记事本到Outlook Express,

然后它出来OK:


int main()

{

int x = 1;

返回0;

}


-

John Carson



You''d think so wouldn''t you. As it happens, it is not true if pasting
directly from the VC++ IDE into Outlook Express. Observe below how "spaces
only" code comes out (the two lines inside main are both indented in the
VC++ IDE with 4 spaces):

int main()
{
int x = 1;
return 0;
}

If you follow a more indirect route: VC++ to Notepad to Outlook Express,
then it comes out OK:

int main()
{
int x = 1;
return 0;
}

--
John Carson


2005-10-25,John Carson< jc ********* *******@netspace.net.au>写道:
On 2005-10-25, John Carson <jc****************@netspace.net.au> wrote:
" Neil Cerutti" <乐******* @ email.com>在消息中写道
新闻:sl ********************** @ FIAD06.norwich.edu
"Neil Cerutti" <le*******@email.com> wrote in message
news:sl**********************@FIAD06.norwich.edu
2005-10 -25, sh ************* @ gmail.com < sh ************* @ gmail.com>
写道:
On 2005-10-25, sh*************@gmail.com <sh*************@gmail.com>
wrote:

(对不起格式错误 - 我复制/直接从我的
IDE中粘贴...)

(Sorry for bad formatting - I copy/pasted straight out of my
IDE...)



检查您的IDE是否有将标签转换为空格的选项。那个
会在发布代码时纠正任何问题。



Check if your IDE has an option to convert tabs to spaces. That
will correct any problems when posting the code.



你会这样想的不是你。实际上,如果直接从VC ++ IDE粘贴到Outlook Express中则不然。请注意以下仅限空间的方式。代码出来(main中的两行都在VC ++ IDE中缩进,有4个空格):

int main()
{
int x = 1 ;
返回0;
}



You''d think so wouldn''t you. As it happens, it is not true if pasting
directly from the VC++ IDE into Outlook Express. Observe below how "spaces
only" code comes out (the two lines inside main are both indented in the
VC++ IDE with 4 spaces):

int main()
{
int x = 1;
return 0;
}




呀!

-

Neil Cerutti



Yikes!
--
Neil Cerutti


这篇关于为什么address-of-char会给我带来非终止的c-string乱码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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