二维动态数组定义及二维静态数组与**P的区别

矩力集成2008年校园招聘笔试题:动态申请一个二维数组存储图像阵列

传统的解决方案是分配一个指针数组, 然后把每个指针初始化为动态分配的 ``列"。 以下为一个二维的例子:

//typedef int (*arraypoiter)[ncolumns];

int **dynamic_alloc_arrays(unsigned int nrows,unsigned int ncolumns)

{

unsigned int i;

int **array = (int **)malloc(nrows * sizeof(int *));

for(i = 0; i < nrows; i++)

array[i] = (int *)malloc(ncolumns * sizeof(int));

printf("array=0x%x/n",(int)array);

for(i=0;i<nrows;i++)

{

printf("array[%d]=0x%x/n",i,(int)array[i]);

}

printf("/n");

return array;

}

void main(void)

{

int **test_allocate;

unsigned int nrows=3;

unsigned int ncolumns=4;

test_allocate = dynamic_alloc_arrays(nrows,ncolumns);

printf("test_allocate=%x/n",test_allocate);

}

/*

array=911c70

array[0]=911c30

array[1]=911bf0

array[2]=911bb0

test_allocate=911c70

*/

当然, 在真实代码中, 所有的 malloc 返回值都必须检查。你也可以使用 sizeof(*array) 和sizeof(**array) 代替 sizeof(int *) 和 sizeof(int)(因为*array的类型为int *,**array的类型为int)。

你可以让数组的内存连续, 但在后来重新分配列的时候会比较困难, 得使用一点指针算术:

int **dynamic_alloc_arrays(unsigned int nrows,unsigned int ncolumns)

{

unsigned int i;

int **array = (int **)malloc(nrows * sizeof(int *));

array[0] = (int *)malloc(nrows * ncolumns * sizeof(int));

for(i = 1; i < nrows; i++)

array[i] = array[0] + i * ncolumns;

printf("array=0x%x/n",(int)array);

for(i=0;i<nrows;i++)

{

printf("array[%d]=0x%x/n",i,(int)array[i]);

}

printf("/n");

return array;

}

void main(void)

{

int **test_allocate;

unsigned int nrows=3;

unsigned int ncolumns=4;

test_allocate = dynamic_alloc_arrays(nrows,ncolumns);

printf("test_allocate=%x/n",test_allocate);

}

/*

array=911c70

array[0]=911c10

array[1]=911c20

array[2]=911c30

test_allocate=911c70

*/

在两种情况下, 动态数组的成员都可以用正常的数组下标 arrayx[i][j] 来访问 (for 0 <= i <nrows 和 0 <= j <ncolumns)。

另一种选择是使用数组指针:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

但是这个语法变得可怕而且运行时最多只能确定一维。因为NCOLUMNS必须为定值

××××××××××××××××××××××××××××××××××××××

C语言里,数组名是被看作指针来使用的,一维数组是指针,二维数组是指向指针的指针,三维是......... 真的是这样的吗??看下面的例子:

void show (int * * info, int x, int y) //打印一个x*y的数组的内容

{

int i, j;

for (i=0;i<x;i++)

{

for (j=0;j<y;j++)

{

printf ("%d ",info[i][j]);

}

printf ("/n");

}

}

void Function (void)

{

int as[10][10];

show (as,10,10);

// error C2664: 'show' : cannot convert parameter 1 from 'int [10][10]' to 'int ** ' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

}

在C中没有安全类型检查,上述程序只是warning,但是程序运行会崩溃

在C++中,根本就无法编译通过,即as[10][10]和int * *根本不是一个类型

为什么?在c中,二维数组虽然是定义为指向指针的指针,但是实际上被指向的指针是不存在的,即没有一个内存来存储这个指针,只是在执行as [n]时返回一个指针罢了,as所指的不过是存放数组内容的地址!!

实际上从上面**P和动态二维数组的使用即可看出来,**P和静态二维数组的本质区别!

转载于:https://www.cnblogs.com/xmphoenix/archive/2011/11/21/2257709.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380087.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

置换元素和非置换元素_循环置换数组元素的C程序

置换元素和非置换元素Problem statement: Write a c program to cyclically permute the element of an array. (In right to left direction). Array should be taken as input from the user. 问题陈述&#xff1a;编写一个c程序来循环置换array的元素 。 (从右到左方向)。 数…

最新Asp.net源码推荐列表(4月7日)

好久没有在cnblogs给大家发布asp.net源码了&#xff0c;把最近整理的一些发给大家&#xff0c;希望对大家有所帮助&#xff0c;以后争取保持每周发布&#xff01;- WOBIZ第一季1.2版源码 Hits:29 2008-4-7 [结构图] [^][VS2005Access] 电子商务2.0软件是窝窝团队基于对互联网…

远控免杀专题(23)-SharpShooter免杀

转载&#xff1a;https://mp.weixin.qq.com/s/EyvGfWXLbxkHe7liaNFhGg 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/mete…

MySQL 发展史

一.MySQL 标志说明MySQL的海豚标志的名字叫“sakila”&#xff0c;它是由MySQL AB的创始人从用户在“海豚命名”的竞赛中建议的大量的名字表中选出的。获胜的名字是由来自非洲斯威士兰的开源软件开发者Ambrose Twebaze提供。根据Ambrose所说&#xff0c;Sakila来自一种叫SiSwat…

scanf读取字符_在C语言中使用scanf()读取整数时跳过字符

scanf读取字符Let suppose, we want to read time in HH:MM:SS format and store in the variables hours, minutes and seconds, in that case we need to skip columns (:) from the input values. 假设&#xff0c;我们要读取HH&#xff1a;MM&#xff1a;SS格式的时间 &…

An Algorithm Summary of Programming Collective Intelligence (3)

k-Nearest Neighbors kNN(不要问我叫什么)PCI里面用kNN做了一个价格预测模型&#xff0c;还有一个简单的电影喜好预测。简单来说就是要对一个东西做数值预测&#xff0c;就要先有一堆已经有数值的东西&#xff0c;从里面找出和要预测的东西相似的&#xff0c;再通过计算这些相似…

远控免杀专题(24)-CACTUSTORCH免杀

转载&#xff1a;https://mp.weixin.qq.com/s/g0CYvFMsrV7bHIfTnSUJBw 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/mete…

DOM快捷键

DOM所有的命令(CMD) 步骤/方法 cmd命令大全&#xff08;第一部分&#xff09;winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 wscript--------windows脚本宿主设置 write----------写字板 winmsd---------系统…

病毒的手工排除与分析(更新完毕)

作者简介杨京涛    8年以上的IT行业经验&#xff0c;理解企业需求&#xff0c;有企业ERP软件部署规划能力&#xff0c;有综合布线网络规划和管理能力。熟悉软件以及各类硬件&#xff0c;电话程控设备&#xff0c;各类网络设备的管理维护。有编程基础,熟悉VBA、脚本、批处理…

JavaScript中的String substring()方法和示例

JavaScript | 字符串substring()方法 (JavaScript | String substring() Method) The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string. JavaScript中的Str…

Java——方法(练习九九乘法表)

public static void(int,byte…) xxx(int a,int b) 修饰符 返回值类型 方法名(参数类型 参数名1&#xff0c;参数类型 参数名2…)&#xff5b; 方法体语句&#xff1b; return 返回值&#xff1b; &#xff5d; public class fangfa {public static void main(String[] ar…

UVA 10405-Longest Common Subsequence

最长公共子序列&#xff0c;值得注意的是这道题不能用scanf读字符串。 #include<stdio.h>#include<string.h>#define MAXD 1005char s1[MAXD], s2[MAXD];int dp[MAXD][MAXD];int max( int a, int b){return a > b ? a : b;}void solve(){int len1 strlen(s1 …

对初学者的几点建议

http://www.cnblogs.com/thcjp/archive/2007/06/14/783157.html 天轰穿vs2005入门.net2.0系列视频教程推出已经有接近8个月了&#xff0c;这期间我收到非常多的反馈&#xff0c;我只能用非常来形容&#xff0c;呵呵&#xff0c;当然也了解了很多人的心理和学习方法。但是很遗憾…

系统固件升级_固件和操作系统之间的差异

系统固件升级固件 (Firmware) Firmware is somewhere similar to software but it is not a software. Somehow it is a modified form of software. 固件与软件相似&#xff0c;但不是软件。 不知何故&#xff0c;它是软件的修改形式。 Firmware is fixed data or code that …

cobalt strick 4.0 系列教程 (5)--- 获取立足点

https://blog.ateam.qianxin.com/CobaltStrike4.0%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C_%E4%B8%AD%E6%96%87%E7%BF%BB%E8%AF%91.pdf 0x01 客户端 System Profiler [即探针] System Profiler 是一个为客户端攻击提供的侦察工具。这个工具启动一个本地的 web 服务器&#xff0…

[转]sql,N/$/#/@的含义和作用

declare sql nvarchar(4000)set sql Nselect TotalRecordscount(*) from N( sqlFullPopulate N) a EXEC sp_executesql sql,NTotalRecords int output, TotalRecords output 问题&#xff1a;sql 后面有个N, N 起什么作用? 答案&#xff1a; 加上 N 代表存入数据库时…

Java——方法重载(overload)(比较两个数据是否相等)

重载&#xff1a;方法名相同&#xff0c;参数列表不同&#xff0c;与返回值类型无关 重载的分类&#xff1a; 1&#xff0c;参数个数不同 ①&#xff0c;④&#xff0c;⑤&#xff0c;⑥&#xff1b; 2&#xff0c;参数类型不同 ①&#xff0c;②&#xff0c;③、 ⑤&#x…

scala怎么做幂运算_Scala幂(幂)函数示例

scala怎么做幂运算Scala programming language has a huge set of libraries to support different functionalities. Scala编程语言具有大量的库来支持不同的功能。 scala.math.pow() (scala.math.pow()) The pow() function is used for the exponential mathematical opera…

frame--转载

所谓框架便是网页画面分成几个框窗&#xff0c;同时取得多个 URL。只 要 <FRAMESET> <FRAME> 即可&#xff0c;而所有框架标记 要放在一个总起的 html 档&#xff0c;这个档案只记录了该框架 如何划分&#xff0c;不会显示任何资料&#xff0c;所以不必放入 <…

cobalt strick 4.0 系列教程(6)Payload Artifact 和反病毒规避

0x01 哲学 Strategic Cyber 责任有限公司会定期回答有关规避的问题。Cobalt Strike 是否能够绕过 AV 产品&#xff1f;它能绕过哪些 AV 产品&#xff1f;它多久检查一次&#xff1f; Cobalt Strike 默认的 Artifact 可能会被大多数终端安全解决方案拦截。规避不是 Cobalt Str…