malloc : malloc « stdlib.h « C Tutorial






ItemValue
Header filestdlib.h
Declarationvoid *malloc(size_t size);
Returnreturns a pointer to the memory. If there is insufficient memory, malloc() returns a null pointer.


It is important to verify that the return value is not null before using it.

Allocate sufficient memory to hold structures of type addr:

#include<stdio.h>

  struct addr {
    char name[40];
    char street[40];
    char city[40];
    char state[3];
    char zip[10];
  };

  main()
  {
    struct addr *p;

    p = malloc(sizeof(struct addr));

    if(p==NULL) {
      printf("Allocation Error\n");
      exit(1);
    }
    return p;
  }








23.19.malloc
23.19.1.malloc