I have written program to understand dynamic memory allocation for array. I have few doubts with dynamic array
Please Take a look at this program
Do you see any mistake in program ?
What is difference between cast and without cast
if I am replacing one line with another in program. output doesn't change
What's the specific use of "with a cast" and "without a cast in program" ?
Please Take a look at this program
C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int * pointer; /*Pointer to array. */
int i, size;
printf( "Size of array : " );
scanf( "%d", &size ); /*Get size of array.*/
pointer = (int *) malloc(size * sizeof (int));
if( pointer == NULL ) /*Check for failure. */
{
printf( "Can't allocate memory!" );
}
printf( "Allocated array! \n" );
for ( i = 0; i < size; i++ )
{
printf( "Enter element : " );
scanf( "%d", &pointer[i] );
}
for ( i = 0; i < size; i++ )
printf( "%d ", pointer[i] );
return 0;
}
What is difference between cast and without cast
C:
int * pointer;
pointer = malloc(10 * sizeof(int)); /* without a cast */
pointer = (int *)malloc(10 * sizeof(int)); /* with a cast */
What's the specific use of "with a cast" and "without a cast in program" ?
Last edited: