n no of array in c using malloc

Solutions on MaxInterview for n no of array in c using malloc by the best coders in the world

showing results for - "n no of array in c using malloc"
Evann
01 Jun 2018
1#include <stdio.h>
2
3int main()
4{
5    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
6    int n, i, *ptr, sum = 0;
7
8    printf("\n\nEnter number of elements: ");
9    scanf("%d", &n);
10
11    // dynamic memory allocation using malloc()
12    ptr = (int *) malloc(n*sizeof(int));
13
14    if(ptr == NULL) // if empty array
15    {
16        printf("\n\nError! Memory not allocated\n");
17        return 0;   // end of program
18    }
19
20    printf("\n\nEnter elements of array: \n\n");
21    for(i = 0; i < n; i++)
22    {
23        // storing elements at contiguous memory locations
24        scanf("%d", ptr+i);    
25        sum = sum + *(ptr + i);
26    }
27
28    // printing the array elements using pointer to the location
29    printf("\n\nThe elements of the array are: ");
30    for(i = 0; i < n; i++)
31    {
32        printf("%d  ",ptr[i]);    // ptr[i] is same as *(ptr + i)
33    }
34
35    /* 
36        freeing memory of ptr allocated by malloc 
37        using the free() method
38    */
39    free(ptr);
40
41    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
42    return 0;
43}
queries leading to this page
array with malloc in cexample for callocwrite a c program to display second min number in an array user enter integers and you need to calculate the size of array and allocate runtime memory for arraywrite a c program to display second min number in an array user enter integers and you need to calculate the size of array and allocate run time memory for arraywrite a c program that take five numbers from user and find largest element using dynamic memory allocation note 3a please do not compare integers directly your program must result on the basis of memory allocated to each number c how to reserve a memory using mallocwrite a c program to find the largest and smallest using dynamic memory allocationwhy calloc 28 29 function is used in c programs 3f allocate specified number of bytes allocate specified number of bytes and initialize them to zero it free up the allocated memory space it expand or shrink the allocated memory spacewrite a program to accept 27n 27 28taken from test data 29 numbers of elements using dynamic memory allocation and then print the element at position 27p 27 28taken from test data 29 after sorting the elements in ascending order allocate contiguous memory space at run time to compute the sum of integers givendynamic array in c programallocate array with 0 using mallocwrite a c program to study the allocation of memory by applyingc proram to find total memory consumed to store the numbers in an arraylowest and highest using dynamic memory allocationmalloc int array size in cwrite out a program in c with dynamic memory to search an element in a dynamic array of n numbers dynamic memory allocation for an array in cmalloc in arraywrite out a program in c with dynamic memory to sort a dynamic array of n numbers how to use malloc to get a arrayfree allocated memory in ctaking array input using malloc in c6 write the function used for dynamic memory allocation 3f 2ahow to store n numbers to memory in cmalloc a n int array cwrite a c program to fin the largest and smallest using malloc functiondynamic memory allocation for integer array in csmallest and largest using dynamic memory allocationallocate space using mallocn no of array in c using malloc