![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
C++初学者提问关于Visual Studio 6.0出错问题。
#include<iostream>usingnamespacestd;intmain(){int*myBalls=8;cout<<"myballsare:"<<*myB...
#include <iostream>
using namespace std;
int main()
{
int *myBalls=8;
cout<<"my balls are :"<<*myBalls<<endl;
cout<<"Address is :"<<myBalls<<endl;
*myBalls=*myBalls+1;
cout<<"Now,my balls are :"<<*myBalls<<endl;
cin.get();
return 0;
}
我写的代码是上面的,错误提示是下面的,没加*号就没错误,请教大侠怎么定义指针?
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
main.cpp
D:\Microsoft Visual Studio\MSDev98\MyProjects\test\main.cpp(5) : error C2440: 'initializing' : cannot convert from 'const int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
test.exe - 1 error(s), 0 warning(s)
回复2楼的,我是照着书抄的,难道书上真的错吗?求指教下指针类型的应用。 展开
using namespace std;
int main()
{
int *myBalls=8;
cout<<"my balls are :"<<*myBalls<<endl;
cout<<"Address is :"<<myBalls<<endl;
*myBalls=*myBalls+1;
cout<<"Now,my balls are :"<<*myBalls<<endl;
cin.get();
return 0;
}
我写的代码是上面的,错误提示是下面的,没加*号就没错误,请教大侠怎么定义指针?
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
main.cpp
D:\Microsoft Visual Studio\MSDev98\MyProjects\test\main.cpp(5) : error C2440: 'initializing' : cannot convert from 'const int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
test.exe - 1 error(s), 0 warning(s)
回复2楼的,我是照着书抄的,难道书上真的错吗?求指教下指针类型的应用。 展开
展开全部
int *myBalls=8;这句本身就是错误的(定义的是指针类型却赋予了一个整形数值).定义指针应该是这样:
int n=8;
int *myBalls=&n;
或者
int*myBalls;
myBalls=&n;
int n=8;
int *myBalls=&n;
或者
int*myBalls;
myBalls=&n;
展开全部
int *myBalls定义了一个myballs的指针,指针其实是一个地址你不能直接让地址等于8啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把
*myBalls
改成
myBalls
就是去掉前面的*号
*myBalls
改成
myBalls
就是去掉前面的*号
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
指针不能直接赋值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询