gpt4 book ai didi

c++ - 数组在 C++ 函数中有垃圾值!

转载 作者:行者123 更新时间:2023-11-28 06:56:56 27 4
gpt4 key购买 nike

我有以下功能:

  int *array1=new int [3];
void My_function()
{
My_set(array1);
for(int i=0;i<3;i++){ //The output is 5 15 55
cout<<array1[i]<<endl;
}
Display (array1)

}

void My_set(int *array1)
{
array1[0]=5;
array1[1]=15;
array1[2]=55;
}

void Display(int *array1){
for(int i=0;i<3;i++){
cout<<array1[i]<<endl; //The output is Garbage -842150451
}

}

注意:这个问题发生在复杂的项目中,但我用简单的代码展示了我的问题!谢谢 :)

最佳答案

你需要像这样声明你的数组 int *array1 = new int[3];

关于c++ - 数组在 C++ 函数中有垃圾值!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037166/

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