malloc() allocates enough storage for a structure. The size of the structure is determined by using the sizeof operator.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() /*from w w w . j av a2 s . c o m*/ { struct Product { char symbol[5]; int quantity; float price; }; struct Product *purchase; /* Create structure in memory */ purchase=(struct Product *)malloc(sizeof(struct Product)); if(purchase==NULL) { puts("Some kind of malloc() error"); exit(1); } /* Assign structure data */ strcpy(purchase->symbol,"ABCD"); purchase->quantity=100; purchase->price=801.19; /* Display database */ puts("Investment Portfolio"); printf("Symbol\tQuantity\tPrice\tValue\n"); printf("%-6s\t%5d\t%.2f\t%.2f\n", purchase->symbol, purchase->quantity, purchase->price, purchase->quantity*purchase->price); return(0); }