Apply for Zend Framework Certification Training

c



< Dynamic memory allocation Passing pointers to functions and returning pointers from functions >



Practice problems and solutions on malloc() in c

1. Allocate Memory for an Integer
Problem:
Write a program to dynamically allocate memory for one integer and print its value.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *ptr;
    ptr = (int*) malloc(sizeof(int));
    if(ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    *ptr = 25;
    printf("Value = %d\n", *ptr);
    free(ptr);
    return 0;
}
2. Array Allocation using malloc()
Problem:
Allocate memory for an array of 5 integers and take input from the user.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *arr, i;
    arr = (int*) malloc(5 * sizeof(int));
    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    printf("Enter 5 numbers:\n");
    for(i = 0; i < 5; i++) {
        scanf("%d", &arr[i]);
    }
    printf("You entered:\n");
    for(i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}
3. Sum of n Numbers using malloc()
Problem:
Take n from user, allocate memory dynamically, and find sum.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *arr, n, i, sum = 0;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    arr = (int*) malloc(n * sizeof(int));
    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    for(i = 0; i < n; i++) {
        printf("Enter number %d: ", i+1);
        scanf("%d", &arr[i]);
        sum += arr[i];
    }
    printf("Sum = %d\n", sum);
    free(arr);
    return 0;
}
4. Find Maximum Element
Problem:
Dynamically allocate array and find the maximum element.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *arr, n, i, max;
    printf("Enter size: ");
    scanf("%d", &n);
    arr = (int*) malloc(n * sizeof(int));
    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    max = arr[0];
    for(i = 1; i < n; i++) {
        if(arr[i] > max) {
            max = arr[i];
        }
    }
    printf("Maximum = %d\n", max);
    free(arr);
    return 0;
}
5. Reverse an Array using malloc()
Problem:
Dynamically allocate memory and reverse the array.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *arr, n, i;
    printf("Enter size: ");
    scanf("%d", &n);
    arr = (int*) malloc(n * sizeof(int));
    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    printf("Enter elements:\n");
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    printf("Reversed array:\n");
    for(i = n - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }

< Dynamic memory allocation Passing pointers to functions and returning pointers from functions >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top