Created
May 25, 2019 13:59
-
-
Save LukeDavitt/8beb686345e2388b37a7121e008727e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
int n = 1000, i, *ptr, sum = 0; | |
ptr = (int*) calloc(n, sizeof(int)); | |
if(ptr == NULL) | |
{ | |
printf("Error! memory not allocated."); | |
exit(0); | |
} | |
for(i = 0; i < n; ++i) | |
{ | |
*ptr++ = i * 655; | |
sum += *(ptr + i); | |
} | |
free(ptr); | |
// Get the number of elements for the array | |
n = 10000; | |
// Dynamically allocate memory using malloc() | |
ptr = (int*)malloc(n * sizeof(int)); | |
// Check if the memory has been successfully | |
// allocated by malloc or not | |
if (ptr == NULL) { | |
printf("Memory not allocated.\n"); | |
exit(0); | |
} | |
else { | |
// Get the elements of the array | |
for (i = 0; i < n; ++i) { | |
ptr[i] = i + 30000; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment