gpt4 book ai didi

c - 未初始化 C Visual 中使用的指针变量

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:44 26 4
gpt4 key购买 nike

void main(){
float a = 5;
float *test;
*test = a;
}

这不会编译。为什么?我的意思是立即有一个错误,即指针变量在没有初始化的情况下无法使用。

这是错误 http://i.imgur.com/TDiYOTU.png 的链接

最佳答案

void main(){
float a = 5;
float *test; // now the pointer test contains a random value
// and because it can be anything it is not safe to access that memory
*test = a; // not safe!. Dereferencing a random address will give garbage.
}

如果您希望指针具有变量 a 的地址,请使用 test = &a

关于c - 未初始化 C Visual 中使用的指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com