Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<stdlib.h>
- void printArr(int *, int);
- int main()
- {
- int n, i;
- printf("Size of array: ");
- scanf("%d", &n);
- if(n<0)
- {
- printf("Memory allocation failed");
- exit(0);
- }
- int *my_array;
- my_array = (int*) malloc(n * sizeof(int)); //dynamic allocation of memory
- if(my_array == NULL)
- {
- printf("Error! memory not allocated."); //if memory not allocated terminate the program
- exit(0);
- }
- *my_array = 1;
- for(i =1; i<n; i++)
- *(my_array+i) = i*100;
- printArr(my_array, n);
- free(my_array);
- return 0;
- }
- void printArr(int * arr, int n){
- int i;
- for(i=0;i<n;i++)
- {
- printf("%d\n",arr[i]); //printing first n elements of array
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement