C语言 malloc动态申请内存,存放数组

代码

#include<stdio.h>
#include<stdlib.h>
int *plusOne()
{
    int *array = NULL;
    array = (int *)malloc(12);
    *(array+0)=6;
    *(array+1)=7;
    *(array+2)=8;
    return array;
}
int main()
{
	int i;
	int digits[]={1,2,3};
	int *array;
	array=plusOne();
	for(i=0;i<3;i++)
	{
		printf("%d ",array[i]); 
	}
	printf("成功");
} 

进阶版

#include<stdio.h>
#include<stdlib.h>
int *plusOne(int* digits, int digitsSize, int* returnSize)
{
	int *ret_array = NULL;
	ret_array = (int *)malloc(*returnSize * 4);
	*(ret_array + 0) = 6;
	*(ret_array + 1) = 7;
	*(ret_array + 2) = 8;
	return ret_array;
}
int main()
{
	int i;
	int digits[] = { 1,2,3 };
	int *array;
	int returnSize=3;
	array = plusOne(digits, 3, &returnSize);
	for (i = 0; i < 3; i++)
	{
		printf("%d ", array[i]);
	}
	printf("成功");
	system("pause");
}

运行结果

6 7 8 成功
--------------------------------
Process exited after 0.09513 seconds with return value 0
请按任意键继续. . .
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页