Sunday, 6 May 2012

Pointers and Structs in C


#include <stdio.h>
#include <string.h>


struct  s {
char *title;
int *id;
float price;
};


static char  product[] = "Parle-G";
static float price1 = 5.00;
static int   id = 773;


void printrecord( struct s * );


void printrecord( struct s *goods )
{
printf("Name = %s\n", goods->title );
printf("ID = %d\n", *goods->id);
printf("Price = %.2f\n", goods->price );
goods->title = &product[0];
goods->id = &id;
goods->price = price1;
}


main()
{
int code = 123, num;
char title[] = "Glucose";
struct s product;


product.id = &code;
product.price = 4.50;
product.title = title;
num = *product.id;
printrecord( &product );
printf("Name = %s\n", product.title );
printf("ID = %d\n", *product.id);
printf("Price = %.2f\n", product.price );
getch();
return 0;
}

No comments:

Post a Comment