Thinking in c++ exercise 4-26 关于二维数组指针

题目描述:

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;
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值