指针基本问题 [英] pointers basic question

查看:85
本文介绍了指针基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char * p = "hello";
cout<<p; 
cout<<p+1;
cout<<*p;


输出


output

hello
105
h


推荐答案

有些奇怪的地方,在这里:
根据标准,您的编译器和/或标准库不是标准的,或者...您做了错字.

关键是cout 和表达式之间的<<运算符.
pchar*,而<<发出它指向的字符序列,直到隐含在"hello"文字定义后面的"\ 0"为止(因此,它显示 hello ).
p+1是指向"h"下一个字符的char *,其作用与以前相同(因此,应已打印 ello .
*p是字符,而<<只需打印即可.
*p+1是一个计算结果为整数的表达式:* p是char"h",而char和int之间的+会将char提升为int并添加整数. "h"是104,加1给出105(并打印).
Something strange, here:
according to the standard, your compiler and or the standard library aren''t standard, or ... you did a typo.

The key is the << operator between cout and the expressions.
p is a char*, and << sends out the sequence of character it points to up to the ''\0'' implicitly appended to the definition of the "hello" literal (hence, it prints hello).
p+1 is a char* pointing to the next of "h", and works the same as before (hence , should had been printing ello.
*p is a character, and << just prints it.
*p+1 is an expression that evaluates to an integer: *p is the char ''h'', and + between a char and an int promotes the char as int and adds the integers. ''h'' is 104, adding 1 gives 105 (and it''s printed).


1)p是指向字符数组的指针,将其赋给cout将打印内容作为字符串
2)将p + 1评估为数组+1中第一个char的内容,即h = 104 + 1
3)* p表示指针所指向的地址的内容是h
1) p is a pointer to a character array and giving it to cout will print the contents as a string
2) p+1 is evaluated as a contents of the first char in the array + 1 ie h=104 + 1
3) *p means the contents of the address the pointer is pointing to which is a character which is h



您好!这是我的问候!


我在vc2005中运行了您的代码.我的电脑赢了7 ..



HELLO! this my greeetings!!!


I ran ur code in vc2005. my pc has win 7..



char * p = "hello";
cout<<p;   // printing the char pointer wihtout any condition
cout<<p+1; // printing the pointer skipping the first character
cout<<*p;// printing the value at the address char p[0]. which is p[0].







我得到了预期的以下答案:

你好
ello
h

请重新构建项目,然后重试!







I got the following anwser which is as expected:

hello
ello
h

please re-build the project and try again!


这篇关于指针基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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