Q:

C program to declare memory for an integer variable dynamically

0

C program to declare memory for an integer variable dynamically

This program is an example of Dynamic Memory Allocation. Here, you will learn how to declare memory at run time for an integer variable? Here we are using malloc() function, which is a predefined function of stdlib.h header file, that is used to declare memory or blocks of memory.

In this example, there is an integer pointer and memory for an integer variable is going to be declaring at run time and the address of dynamically allocated memory will be assigned to the integer pointer. By using this integer pointer we will read an integer number and print the value on the standard output device.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* iVar;

    iVar = (int*)malloc(sizeof(int));

    printf("Now, input an integer value: ");
    scanf("%d", iVar);

    printf("Great!!! you entered: %d.\n", *iVar);

    free(iVar);

    return 0;
}

Output

Now, input an integer value: 5678
Great!!! you entered: 5678.

Statement: int *iVar

It is an integer pointer that will contain the address of dynamically allocated memory.

Statement: iVar=(int*)malloc(sizeof(int))

Here, malloc() will reserve memory of sizeof(int) bytes and assigns the address of reserved memory to the iVar.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C language important programs ( Advance Programs )

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C program to read and print name, where memory for...