ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.