Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
91 views
in C Programming by (176k points)
How do you allocate memory dynamically using pointers in C?

Please log in or register to answer this question.

1 Answer

0 votes
by (176k points)

Dynamic memory allocation in C is done using the malloc() function. It reserves a block of memory of the specified size and returns a pointer to the allocated memory. After using the dynamically allocated memory, it should be freed using the free() function to prevent memory leaks.

Example: Dynamic memory allocation

int main() {
    int *ptr = (int *)malloc(sizeof(int));   // Dynamically allocate memory for an integer

    if (ptr != NULL) {
        *ptr = 10;   // Store a value in the dynamically allocated memory

        printf("Value: %d\n", *ptr);   // Output: 10

        free(ptr);   // Free the dynamically allocated memory
    }

    return 0;
}


Related questions

+1 vote
2 answers
asked May 25, 2023 in C Programming by kvdevika (176k points)
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...