Diktat_ProgKecilBhsC

# Pcp pointprt ptr program utama int main printf

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: / Point P1; Point *P2; /* pointer ke struct, akan dibahas pada pointer */ Point P3; /* latihan baca */ /* Algoritma */ printf ("Contoh mengisi struktur dengan assignment : \n"); printf ("Titik P1, dengan P1.x dan P1.y:\n"); P1.x = 1.1; P1.y = 2.5; printf ("P1.x = %4.1f\nP1.y = %4.1f\n", P1.x, P1.y); /* mengacu pointer ke struct, akan dibahas setelah pointer */ printf ("Titik P2, dengan P2->x dan P2->y :\n"); P2 = (Point *) malloc (sizeof (Point)); P2->x = 9.12; P2->y = 2.567; printf ("P2.x = %f \nP2.y = %f\n", P2->x, P2->y); printf ("Baca Titik P3\n"); scanf ("%f %f", &P3.x, &P3.y); printf ("P3.x = %f \nP3.y = %f \n", P3.x, P3.y); strcpy (Mhs.nama, "Juliette"); Mhs.nim = 7473; Mhs.nilai = 80; printf ("Hasil assignment thd Mhs \n"); printf ("Nama = %s\nNim = %d\nNilai = %d\n", Mhs.nama, Mhs.nim, Mhs.nilai); return 0; } /* File struk2.c */ /* contoh pendefinisian dan pengisian struktur yang mengandung string */ #include <stdlib.h> int main () { struct { char *nama; int nim; int nilai; } Mhs; printf ("\nNama = "); Mhs.nama = (char *) malloc (20 * sizeof(char)); /* alokasi */ gets (Mhs.nama); printf ("\nNIM dan nilai= "); scanf ("%d %d", &Mhs.nim, &Mhs.nilai); printf ("Hasil assignment thd Mhs \n"); printf ("Nama = %s\nNim = %d\nNilai = %d\n", Mhs.nama, Mhs.nim, Mhs.nilai); return 0; } /* File : struk3.c */ /**** Definisi TYPE & Variabel GLOBAL */ /* contoh membedakan variabel, tag dan nama type */ typedef struct point /* nama tag */ { float x; float y;}; typedef struct point *pointprt; /* pointptr : nama type */ typedef struct tagx /* nama tag */ { int i; int j; }namat; /* nama type */ typedef struct tagx *tpoint; /* typedef struct namat *tnm; --> undefined type */ typedef struct { int k; int l; } nama; /* type */ typedef nama cc; typedef nama *cp; /*--> ok sebab tidak pakai TAG */ typedef int infotype; typedef struct tElmtlist *address; /* pointer dituliskan ke “tag” */ typedef struct tElmtlist {infotype info; struct tElmtlist *next; } ElmtList; ElmtList elmq; address P; /* tElmtlist el; ---> error, t Elmtlist bukan nama type, tapi nama tag */ cp pcp; pointprt ptr; /*** Program Utama ****/ int main () { printf ("Hello, hanya coba deklarasi"); return 0; } UNION /* File union.c */ int main() { /* KAMUS */ /* Definisi Type */ typed...
View Full Document

## This document was uploaded on 03/15/2014 for the course IF 2030 at Institut Teknologi Bandung.

Ask a homework question - tutors are online