March 2019
Intermediate to advanced
708 pages
17h 9m
English
Let's look at some example code that allocates a pointer variable, i, then assigns memory to it using malloc(). We allocate a single integer behind an int* pointer. After allocation, we store a value inside int, using the dereferencing operator *:
// CREATING AND ALLOCATING MEMORY FOR AN INT VARIABLE i // Declare a pointer variable i int * i; // Allocates system memoryi = ( int* )malloc( sizeof( int ) );// Assign the value 0 into variable i *i = 0; // Use the variable i, ensuring to // use dereferencing operator * during use printf( "i contains %d", *i ); // RELEASING MEMORY OCCUPIED BY i TO THE SYSTEM // When we're done using i, we free the memory // allocated for it back to the system. free( i ); // Set the pointer's reference ...
Read now
Unlock full access