C ++中char *和int *之间的行为差​​异 [英] Difference in behavior between a char* and an int* in C++

查看:59
本文介绍了C ++中char *和int *之间的行为差​​异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有两个片段。需要理解为什么char *成功而int *给出错误。试着理解char *和int *之间的行为差​​异*

先谢谢你的帮助。



什么我试过了:



摘录1:



int * p;

p = 19;




cout<<p val<< * p<< endl;

cout<<p addr<<& p<< endl;



o / p:

-----

main.cpp:在函数'int main()'中:

main.cpp:14:3:错误:从'int'无效转换为'int *'[-fpermissive]

p = 19;


^〜

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



摘录2:



char * a;

a =fubar;




cout<<a val =< < * a<< endl;

cout<<a =<< a<< endl;

cout<< a addr =<<& a<< endl;



o / p:

-----



$ g ++ -o main * .cpp

main.cpp:在函数'int main()'中:

main.cpp:14:8:警告:ISO C ++禁止将字符串常量转换为'char *'[-Wwrite-strings] < br $>
a =fubar;

^ ~~~~~~

$ main

a val = f

a whole = fubar

a addr = 0x7ffed3404898

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

Have two snippets below . Need to understand why char* is successful while int* gives an error. Trying to understand the difference in behavior between a char* and an int*
Thanks in advance for your help .

What I have tried:

snippet 1 :

int *p;
p=19;


cout<<"p val"<<*p<<endl;
cout<<"p addr"<<&p<<endl;

o/p:
-----
main.cpp: In function ‘int main()’:
main.cpp:14:3: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
p=19;

^~
===============================================================

snippet 2:

char *a;
a="fubar";


cout <<"a val = "<<*a<<endl;
cout <<"a = "<<a<<endl;
cout <<"a addr = "<<&a<<endl;

o/p:
-----

$g++ -o main *.cpp
main.cpp: In function ‘int main()’:
main.cpp:14:8: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
a="fubar";
^~~~~~~
$main
a val = f
a whole = fubar
a addr = 0x7ffed3404898
===============================================================

推荐答案

g ++ -o main * .cpp

main.cpp:在函数'int main()'中:

main.cpp:14: 8:警告:ISO C ++禁止将字符串常量转换为'char *'[-Wwrite-strings]

a =fubar;

^ ~~~~~~
g++ -o main *.cpp
main.cpp: In function ‘int main()’:
main.cpp:14:8: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
a="fubar";
^~~~~~~


main

a val = f

a whole = fubar

a addr = 0x7ffed3404898

=========================================== ====================
main
a val = f
a whole = fubar
a addr = 0x7ffed3404898
===============================================================


消息说明了一切 - 您无法将字符串常量转换为'char *'。原因是它可以改变常数而不允许。要进行编译和运行,将a的声明更改为'const char * a'然后它将起作用。



-edit-



关于代码段1:它被声明为指针,下一行将值19赋给该指针。这可能不是一个有效的地址,而这正是消息所指的内容。此外,您可能并不真正想要指针的地址,而是它的值,它将是它所拥有的地址。这意味着该行应该更改:
The message says it all - you can't convert a string constant to a 'char *'. The reason being that it would then possible to change the constant and that is not allowed. To make this compile and run, change the declaration of a to 'const char *a' and then it will work.

-edit-

Regarding snippet 1 : it is declared as a pointer and the next line assigns the value 19 to that pointer. That is not likely to be a valid address and that is what the message is referring to. Also, you probably don't really want the address of the pointer but its value which would be the address it holds. This means that line should be changed :
cout << "p addr" << p << endl;

,它将显示指针所包含的地址。以下是更好地使用指向整数的指针:

and that will display the address the pointer holds. Here is a better use of a pointer to an integer :

int ivalue = 0;
int* iptr = &ivalue;   // set iptr to the address of ivalue
*iptr = 42;            // set the value of the integer pointed to

的整数值ivalue的值为42.这里的关键是指针的值是它所拥有的地址。要获取该地址的数据,您必须取消引用指针,这就是星号所做的。

now ivalue will have a value of 42. The key thing here is the value of a pointer is the address it holds. To get the data at that address you have to dereference the pointer and that is what the asterisk does.


这篇关于C ++中char *和int *之间的行为差​​异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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