我是C++编程的新手。当我在联机编译器中编译代码时,我得到了Segmentation Fault错误,但是当我尝试在脱机状态下使用Visual Studio Code和g++编译它时(意味着在我的本地机器上),它工作得很好。
我尝试过的Code是
`
#include <iostream>
int main() {
int *ptr;
*ptr = 10;
cout<<*ptr; //Prints 10
cout<<ptr; //Prints Some garbage address
}但是上面的程序不能在联机编译器中运行(在onlinegdb上使用)。我的计算机配置g++ 11 Visual Studio Code 2016
发布于 2021-09-08 09:04:32
这行*ptr = 10;基本上是错误的,因为您不能通过取消引用指针来赋值。
这样做的正确方法是:
#include <iostream>
using namespace std;
int a=10;
int *ptr;
ptr=&a;
cout<<ptr<<endl;
cout<<*ptr<<endl;https://stackoverflow.com/questions/61616196
复制相似问题