Hello
I am punching in the shadows with the structure code and got to evolve a bit since the first day (two weeks ago) I saw the word "C basics".
So this does create the structure, stores the data on user input, and prints results. It is in a loop, because say I wanted to store data for different people and I did an array of structures, but of course, this is unpractical. If someone opens the application, he wants to enter as many or as few data as he wants, so the program cannot loop over a predefined number of times. It would have to sort of: Do you want to enter another structure?
It sounds appealing to me, I would be thinking in some dynamic malloc stuff for the structure and maybe some base flag that when Yes, it loops to produce another structure, upon request I mean.
Oh yes, the question actually is: any hint as to how the program should promt for a Structure to be filled upon opening the application ? and once he has finished, the user gets asked if he wants another one.
Here I create 2 instances of the Parent structure, but like I said, this is not something practical. The number of instances needed is unknown, infinite maybe of a million, so creating an array of structures with a predefined number serves no useful purpose. It needs a way it can replicate structures upon request
Code:
#include <stdio.h>
#include <stdlib.h>
struct MiEstructura // JUST DEFINING THE STRUCTURE
{
char FirstName[20];
char LastName[30];
char * Address[50];
};
struct MiEstructura Instancia[2]; // this is an array of structures
struct MiEstructura *ptr;
int main ()
{
int i;
ptr = &Instancia[0];
for(i=0; i<2; i++)
{
printf("Enter First Name \n");
scanf("%s",ptr->FirstName);
printf("%s\n",ptr->FirstName);
printf("Enter Last Name \n");
scanf("%s",ptr->LastName);
printf("%s\n",ptr->LastName);
printf("Enter Address \n");
scanf("%s",ptr->Address);
fflush(stdin);
printf("%s\n",ptr->Address);
ptr++;
}
//LOOPING ALONG THE STRUCTURE INSTANCE TO GET ALL RESULTS
ptr = &Instancia[0]; // Well I thought that I had to reset it so as to point the pointer to the start of the array
for (i=0; i<2; i++)
{
printf("FirstName %s, LastName %s, Address %s\n", ptr->FirstName, ptr->LastName, ptr->Address);
ptr++;
}
getchar();
return 0;
}