在C中定义一个动态的二维数组
一般来讲两种办法:
第一种:连续内存分配
#include "stdio.h"
#include "stdlib.h"
int main()
{
int x,y;
int *p;
scanf("%d%d",&x,&y);
p=(int *)malloc(x*y*sizeof(int));
.....//这样定义要访问第i行第j列应该用*(p+i*y+j)
free(p);//释放内存
return ;
}
第二种:通过指针数组+二级指针
#include "stdio.h"
int main()
{
int x,y;//x行y列
int i,j;
int **p;
scanf("%d%d",&x,&y);
p=(int **)malloc(x*sizeof(int*));
if(p==NULL)//分配失败
{
//printf("内存分配失败!\n");
exit();
}
for(i=;i<x;i++)
{
p[i]=(int *)malloc(y*sizeof(int));
if(p[i]==NULL)//分配失败
{
//printf("内存分配失败!\n");
for(j=;j<i;j++) //将之前分配好的内存释放掉
free(p[j]); free(p);
exit();
}
}
........//访问第i行第j列:p[i][j] for(i=;i<x;i++)//释放内存
free(p[i]); free(p);
return ;
}
在C中定义一个动态的二维数组的更多相关文章
- 转:用STL中的vector动态开辟二维数组
用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...
- c中使用malloc动态申请二维数组
前言 今天写代码的时候,想要动态的申请一个二维数组空间,思索了一段时间才写出来,这里记录一下吧,以后就不至于再浪费时间了.下面以申请int型数组作为例子: 申请一维数组 一维数组的数组名可以看成数组起 ...
- 首先定义一个5X8的二维数组,然后使用随机数填充满。借助Arrays的方法对二维数组进行排序。
package day02; import java.util.Arrays; import java.util.Random; public class Test01 { public static ...
- C++中动态申请二维数组并释放方法
C/C++中动态开辟一维.二维数组是非常常用的,以前没记住,做题时怎么也想不起来,现在好好整理一下. C++中有三种方法来动态申请多维数组 (1)C中的malloc/free (2)C++中的new/ ...
- 如何在C++中动态建立二维数组(转)
http://blog.sina.com.cn/s/blog_7c073a8d0100qp1w.html http://blog.163.com/wujiaxing009@126/blog/stati ...
- 消除VS中动态申请二维数组C6011,C6385,C6386的警告
动态申请二维数组,无非就是通过指针来实现.@wowpH 过程分三步:1.申请内存,2.使用数组,3.释放内存. 代码如下: /************************************* ...
- C语言动态生成二维数组
# 动态创建二维数组示例 #include "stdlib.h" #include "stdio.h" #include <malloc.h> in ...
- C语言 动态创建二维数组
/*C语言 如何动态创建二维数组 转化为一维数组申请数组,创建和释放都比较简单 */ #include <stdlib.h> #include <stdio.h> #inclu ...
- php数组根据某一个键值,把相同键值的合并生成一个新的二维数组
http://blog.csdn.net/xyzchenxiaolin/article/details/51700485 源数据: $infos = array( array( 'a' => 3 ...
随机推荐
- 引用外部CSS的link和import方式的分析与比较
很多网页中的 CSS 链接与引用是这样写的: <style type="text/css" media="screen"> @import url( ...
- 【LCA】bzoj 2144:跳跳棋
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 248 Solved: 121[Submit][Status][Discuss] ...
- 解决WIN8 磁盘100 活动占用100% win8硬盘一直响
一.先看最终效果: 二.再说解决办法: 1.任务管理器关闭进程 taskhost.exe和类似于taskhostxx.exe开头的进程. 2.在电源管理里面设置2分钟不使用硬盘则关闭硬盘,看我的截 ...
- Interface Serializable
public interface Serializable Serializability of a class is enabled by the class implementing the ja ...
- objective-c宏定义
1.先来几个常用的: // 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ...
- redis 参考
http://redis.readthedocs.org/en/2.4/index.html
- Linux 套接字编程中的 5 个隐患
http://www.ibm.com/developerworks/cn/linux/l-sockpit/ 在 4.2 BSD UNIX® 操作系统中首次引入,Sockets API 现在是任何操作系 ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-003-把用户数据存在数据库
一. 1.It’s quite common for user data to be stored in a relational database, accessed via JDBC . To c ...
- 23.allegro中钻孔[原创]
1.钻孔: NC Parameters,NC Drill, Dill Legend, NC Route ----------------------- ------ ----- ------- --- ...
- 对于eclipse新建maven工程需要注意的地方。
新建项目的时候,如果想配置默认的maven的jre为1.6或者别的. http://hi.baidu.com/hi_hi/item/765ec8bbc49880d384dd79d1 1.cmd命令建立 ...