关于指针c++问题

2025-06-23 07:33:26
推荐回答(6个)
回答1:

明显的错误。
studentptr是个指针变量。你在程序里并没有给它指向内存。由于你是定义的全局变量,所以默认会让它指向NULL。所以你直接用它来访问gpa,就会造成错误,事实上它并没有指向内存,自然也就不能赋值。
你应该这么写 studentptr = new studentType;
然后再studentptr->gpa=12.1;
或者 由于你也定义了一个studentType的变量 ,你可以让这个指针指向这个变量
这么写 studentptr = &student; 然后再赋值

回答2:

没分配内存 刚没仔细看 你的是结构体的指针 要先分配内存

回答3:

studentptr虽然是指向studentType型的指针变量,但是主函数中并未对其赋值,就直接 studentptr->gpa=12.1; 很明显tudentptr指向一个不确定的地址,当然会报错了
改成下面的就好了
studentptr=&student;
studentptr->gpa=12.1
就好了

回答4:

应该这么写
#include
using namespace std;

struct studentType
{
char name[20];
double gpa;

};
studentType student;

int main()
{
studentType *studentptr = new studentType;// new一个
studentptr->gpa=12.1;

system("pause");
delete studentptr;
return 0;

}

回答5:

kyno_yang 分析很对,可以参考。

回答6:

LL的时候。 这个想怎么处理你自己弄吧
vs2005下能编译

#include
#include
using namespace std;

template
struct Node {
auto_ptr> next;
_Ty data;
Node(_Ty data, auto_ptr> next) : data(data), next(next) {}
};

template
class ChainStack {
auto_ptr> top;
public:
ChainStack() : top(0) {}
void push(_Ty data) {
top.reset(new Node<_Ty>(data, top));
}
_Ty pop() {
_Ty temp = top->data;
top = top->next;
return temp;
}
bool empty()
};

int main()
{
ChainStack s;
s.push(1);
s.push(2);
s.push(4);
while (!s.empty())
{
cout << s.pop() << ends;
}
}
另外,站长团上有产品团购,便宜有保证