『转』堆中开辟二维数组

/*
	2012年11月26日20:42:45
	在堆中开辟 5*5 二维数组
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

int main()
{
	int ** p;
	int i, j;
	/*动态申请二维数组空间*/
	p = (int **)malloc(5 * sizeof(int *));
	if (NULL == p)
	{
		printf("动态内存分配失败!\n");
		exit(-1);
	}
	for (i = 0; i < 5; i++)
	{
		p[i] = (int *)malloc(5 * sizeof(int));
		if (NULL == p[i])
		{
			printf("动态内存分配失败!\n");
			exit(-1);
		}
	}

	/*二维数组赋值并输出*/
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 5; j++)
		{
			p[i][j] = 5 * i + j;
			printf(" %2d", p[i][j]);
		}
		printf("\n");
	}
	printf("\n");

	/*释放二维数组所占空间*/
	for (i = 0; i < 5; i ++)
		free(p[i]);
	free (p);

	return 0;
}

 

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/8227535

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();