Skip to content

Instantly share code, notes, and snippets.

@LukeDavitt
Created May 25, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukeDavitt/8beb686345e2388b37a7121e008727e9 to your computer and use it in GitHub Desktop.
Save LukeDavitt/8beb686345e2388b37a7121e008727e9 to your computer and use it in GitHub Desktop.
#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