Answers for "calloc in C++/C"

0

calloc in C++/C

#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
   int *ptr;
   ptr = (int *)calloc(5, sizeof(int));
   if (!ptr) {
      cout << "Memory Allocation Failed";
      exit(1);
   }
   cout << "Initializing values..." << endl
        << endl;
   for (int i = 0; i < 5; i++) {
      ptr[i] = i * 2 + 1;
   }
   cout << "Initialized values" << endl;
   for (int i = 0; i < 5; i++) {
      /* ptr[i] and *(ptr+i) can be used interchangeably */
      cout << *(ptr + i) << endl;
   }
   free(ptr);
   return 0;
}
Posted by: Guest on May-02-2021

Browse Popular Code Answers by Language