这是什么意思? [英] what is meaning of this ?

查看:70
本文介绍了这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<conio.h>

using namespace std;
void main()
{

    cout<<"f"-8<<endl;
    getch();
}

推荐答案

如果将8替换为0,则输出为f,即字符串的内容.

现在...

If you replace the 8 with 0 the output is f which is the content of the string.

Now...

void main()
{
 
    cout << "fy" + 0 <<endl;
    cout << "fy" + 1 <<endl;
    getch();
}



给出输出:

fy
y

为什么?编译器将字符串"fy"创建为以null终止的字符数组.例如:



Gives the output:

fy
y

Why? The compiler creates the string "fy" as a null terminated array of characters. For example:

char myStr[] = "fy";



因此



So

"fy" + 0

可以写为

myStr + 0

等.

因此,在您的示例中,输出将从内存中字符串"f"之前的8个字符处开始,一直持续到遇到第一个NULL字符为止.

由于我们不知道该内存中的内容,因此输出是不可预测的.

etc.

So in your example the output will commence at the address 8 characters before the string "f" in memory and continue till the first NULL character is encountered.

As we do not know what is in this memory the output is unpredictable.


只需将"fy"视为以空字符结尾的指针,例如p ="fy". > 所以"fy" +1表示p + 1,现在p + 1移到了字母"y"
与"fy" -8相同
但是(p-8)不确定是否知道它在内存中的内容.
just treat "fy" as null-terminated as a pointer,for example p="fy".
so "fy"+1 means p+1,and now p+1 moves to the letter "y"
the same as "fy"-8
but (p-8) is not so sure to know it''s content in memory.


这篇关于这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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