Sunday, August 2, 2009

Question for C programming Dynamically allocate an integer array things with 10 elements.?

main()


{


int a[],n;


clrscr();


printf("Enter the array Length:");


scanf("%d",%26amp;n);





for(i=0;i%26lt;n;i++)


{


prinf("Enter the values a %d",i);


scanf("%d",%26amp;a[i]);


}


printf("The Array Vales are:");





int a[],l;


clrscr();


printf("Enter the array Length:");


scanf("%d",%26amp;l);





for(i=0;i%26lt;n;i++)


{


prinf("Enter the values a %d",a[i]);


}





getch();


}








compile and run it :

Question for C programming Dynamically allocate an integer array things with 10 elements.?
to allocate the dynamically memory for 10 integer number by using malloac()and calloc() function
Reply:// The easiest way is right here





void DynmaicAllocOfIntegerArray()


{


const int ArraySize = 10;


int *myArray = malloc(sizeof(int) * ArraySize);





// Do something with your array


myArray[0] = 0;


myArray[1] = 1;


myArray[2] = 2;


myArray[3] = 3;


myArray[4] = 4;


myArray[5] = 5;


myArray[6] = 6;


myArray[7] = 7;


myArray[8] = 8;


myArray[9] = 9;





free(myArray); // Frees your array


}





// Have fun,


// S. B.


No comments:

Post a Comment