#include<stdio.h>
#include<stdlib.h>


void AllocateMemory(int **pGetMemory, int n)
{

    int *p = (int*)malloc(sizeof(int) * n);

    if (p == NULL)
    {
        *pGetMemory = NULL;
    }
    else
    {
        *pGetMemory = p;
    }
}


int main()
{
    int *arr = NULL;
    int len = 10;
    int i = 0;

    //Allocate the memory
    AllocateMemory(&arr, len);

    if (arr == NULL)
    {
        printf("Failed to allocate the memory\n");
        return -1;
    }

    //Store the value
    for (i = 0; i < len; i++)
    {
        arr[i] = i;
    }

    //print the value
    for (i = 0; i < len; i++)
    {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    //free the memory
    free(arr);


    return 0;

}

输入结果如下:

c语言二级指针的使用,malloc内存申请

相关文章:

  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-05-09
  • 2021-04-21
  • 2022-01-09
  • 2021-06-13
猜你喜欢
  • 2021-04-22
  • 2021-11-26
  • 2021-10-22
  • 2021-12-17
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案