Pointing out the horses using Memory allocation in C

 
#include
#include
#include /* For malloc() */

int main(void)
{
struct horse /* Structure declaration */
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
};

struct horse *phorse[50]; /* pointer to structure array declaration */
int hcount = 0; /* Count of the number of horses */
char test = '\0'; /* Test value for ending input */

for(hcount = 0; hcount < 50 ; hcount++ )
{
printf("\nDo you want to enter details of a%s horse (Y or N)? ",
hcount?"nother " : "" );
scanf(" %c", &test );
if(tolower(test) == 'n')
break;

/* allocate memory to hold a structure */
phorse[hcount] = (struct horse*) malloc(sizeof(struct horse));

printf("\nEnter the name of the horse: " );
scanf("%s", phorse[hcount]->name ); /* Read the horse's name */

printf("\nHow old is %s? ", phorse[hcount]->name );
scanf("%d", &phorse[hcount]->age ); /* Read the horse's age */

printf("\nHow high is %s ( in hands )? ", phorse[hcount]->name );
scanf("%d", &phorse[hcount]->height ); /* Read the horse's height */

printf("\nWho is %s's father? ", phorse[hcount]->name );
scanf("%s", phorse[hcount]->father ); /* Get the father's name */

printf("\nWho is %s's mother? ", phorse[hcount]->name );
scanf("%s", phorse[hcount]->mother ); /* Get the mother's name */
}

/* Now tell them what we know. */
for (int i = 0 ; i < hcount ; i++ )
{
printf("\n\n%s is %d years old, %d hands high,",
phorse[i]->name, phorse[i]->age, phorse[i]->height);
printf(" and has %s and %s as parents.",
phorse[i]->father, phorse[i]->mother);
free(phorse[i]);
}
return 0;
}




Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!