TUGAS ASD (RABU 20 Maret 2012)
ALOKASI MEMORI DINAMIS PADA STRUCT Contoh Program struct tanpa malloc()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h> #include <conio.h>
struct motor{ float amps; float volts; };
typedef struct motor motor;
int main() {
motor m;
m.volts = 4.5; m.amps = 6;
cout << "volts =“ << m.volts; cout << “\n amps =“ << m.amps; getch();
}
Lengkapi potongan program dibawah dan Implementasikan: Alokasi memori dinamis pada struct (menggunakan Malloc)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h> #include <conio.h> #include <stdlib.h>
struct motor{ float amps; float volts; };
typedef struct motor motor;
void main() {
motor *m;
// Buat alokasi memory dinamis seukuran struct motor // Cek alokasi memory ...
// isi masing-masing elemen data struct motor dan tampilkan // Free / bebaskan alokasi memory yang dipesan ....