我想读取一个文件并分配一个包含读取值的数组,稍后我将使用它.
这是我得到的:
int main(){
float *values;
somefunction (&values);
values[3]=3; //Works OK but I want to modify it from the other function
}
somefunction(float ** values){
//I read the file here and count the lines
//for the sake of simplicity lets say I got lines=10;
*values = new float[lines]; //Works OK
*values[0]=0; //Works OK because it points to the first or only element
*values[1]=1; //Segmentation Fault
}
Run Code Online (Sandbox Code Playgroud)
为什么从main函数修改新分配的浮点数组是可以的,而不是从分配它的同一函数中修改,我做错了什么?
我很确定我修改数组的语法是错误的,但我不知道为什么......,提前谢谢.
小智 5
更容易阅读......
somefunction(float*& values)
{
values = new float[lines];
values[0] = 0;
values[1] = 1;
}
main()
{
float* values;
somefunction(values);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |