Thinking in c++ exercise 4-26 关于二维数组指针
2016-03-12 13:49
393 查看
题目描述:
Write a function that takes two int arguments,rows and
columns. This function returns a pointer to a dynamicallyallocated two-dimensional array of
float. As a hint, the first call tonew is:
new float[rows][]. This must be followed by multiple callsto new in order to create all the storage for the
rows. Write asecond function that takes this “matrix” and frees the storage usingdelete. Now convert the code into a
struct calledmatrix.
大意是写一个函数:开辟一个float型的二维数组内存空间,并返回指向这个二维数组的指针
关于二维数组指针的详细解释,请参考这篇二维数组指针
Write a function that takes two int arguments,rows and
columns. This function returns a pointer to a dynamicallyallocated two-dimensional array of
float. As a hint, the first call tonew is:
new float[rows][]. This must be followed by multiple callsto new in order to create all the storage for the
rows. Write asecond function that takes this “matrix” and frees the storage usingdelete. Now convert the code into a
struct calledmatrix.
大意是写一个函数:开辟一个float型的二维数组内存空间,并返回指向这个二维数组的指针
关于二维数组指针的详细解释,请参考这篇二维数组指针
#include<iostream> //返回类型为 指向二维数组的指针 float** fun(int r, int c){ float **m = new float*[r]; //m为指向r个float型指针的指针 for(int i=0; i<r; i++){ m[i] = new float[c]; //m[i]是一个指向一维数组的指针 } return m; } int main(){ int raw=5, column=4; float z=0.1; float **matrix = fun(raw, column); for(int i=0; i<raw; i++){ for(int j=0; j<column; j++){ matrix[i][j]=z; z+=1; } } for(int i=0; i<raw; i++){ for(int j=0; j<column; j++){ std::cout << matrix[i][j] << std::endl; } } }
相关文章推荐
- VC++ Excel电子表格读写示例
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- c++ 写时拷贝
- c++ 深浅拷贝(传统写法 xain'dai'xie'fa)
- c语言常见50题 及答案(递归 循环 以及常见题目)
- C++实验1-三个整数输出最大值
- [变态的C++]“迷路”的函数调用错误
- 用C++实现顺序表
- Inputreader.cpp里virtualkey的流程
- C++ 友元类的学习与使用
- Dev C++初始化(默认)代码修改方法
- “位拷贝”和“值拷贝”的区别(c++)
- 【HPU】[1006]DNA
- 四则运算C语言程序
- C++ 通过友元函数对类进行iostream流重载
- C++/C中的预处理命令;
- C++ 面试题
- 《你必须知道的495个C语言问题》读书笔记之第4-7章:指针
- 数值的整数次方
- C++中输入输出缓冲区的刷新