Allocate Memory for an Integer Variable Dynamically in C programming.


IncludeHelp 11 June 2016

In this code snippet we will learn how to declare memory at run time for an integer variable, Dynamic Memory Allocation can be done by malloc() function in C programming language.

C Code Snippet - Dynamic Memory Allocation for an Integer Variable

/*Allocate memory for an integer dynamically.*/

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

int main()
{
	int *ptr;
	
	ptr=(int*)malloc(1*sizeof(int));
	
	*ptr=10;
	
	printf("value is: %d, address if: %u\n",*ptr, ptr);
	
	free(ptr);
	
	return 0;
}
    

    value is: 10, address if: 15020048



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.