C中如何为第二维长度固定的二维数组分配内存

tt posted @ 2008年8月14日 03:08 in C语言编程 , 3275 阅读

在所写的代码中,有时需要为一个二维数组分配内存,该二维数组的第一维长度不定,而第二维是固定(类似arr[n][3]的数组)。我们可以想到的是用双指针代替数组,当然可以;也可以直接对n赋值后,直接定义arr[n][3](C99标准支持),但这里要说的是另一种方法。

这里以将点云数据读入二维数组为例,由于点云点数n不定,可以确定的是,点是三维点,可以用以下方式定义并分配内存:

double (*arr)[3] = malloc (n*3*sizeof(double));

但在VC编译环境下,将会报错——无法从“void *”转换为“double (*)[3]”,此时应该在malloc函数之前进行类型转换,应该如何转换呢?怎样转换才能成double (*)[3]类型呢,可以进行如下转换:

double (*arr)[3] = (double ((*)[3]))malloc (n*3*sizeof(double));

搞定!:)。

Avatar_small
Junior Dakil Result 说:
2022年8月28日 18:02

Government of Bangladesh, Secondary and Higher Secondary Education Board has successfully completed those Junior School Certificate and Junior Dakil (Grade-8) annual final examination tests between 2nd to 11th November 2022 at all selected centers across the country along with Dhaka Division, <a href="https://bdjscresult2018.com/jsc-result-dhaka-board-with-marksheet-download/">Junior Dakil Result Dhaka Board</a> and JSC & JDC Exams 2022 also completed successfully at all district schools of the Dhaka Board. According to the reports, there are a huge number of students are appeared for this Grade 8th standard terminal exams under Dhaka division and they are waiting for JSC Result 2022 Dhaka Board, right now the Secondary and Higher Secondary Education Board Dhaka has conducted the evaluation of answer sheet scripts to calculate subject wise and total marks of the student to announce JSC Result 2022 Dhaka Board.

Avatar_small
Junior Dakil Result 说:
2022年8月28日 18:05

Government of Bangladesh, Secondary and Higher Secondary Education Board has successfully completed those Junior School Certificate and Junior Dakil (Grade-8) annual final examination tests between 2nd to 11th November 2022 at all selected centers across the country along with Dhaka Division, Junior Dakil Result Dhaka Board and JSC & JDC Exams 2022 also completed successfully at all district schools of the Dhaka Board. According to the reports, there are a huge number of students are appeared for this Grade 8th standard terminal exams under Dhaka division and they are waiting for JSC Result 2022 Dhaka Board, right now the Secondary and Higher Secondary Education Board Dhaka has conducted the evaluation of answer sheet scripts to calculate subject wise and total marks of the student to announce JSC Result 2022 Dhaka Board.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter