Thread: Structure, coded it, works, want to "upgrade" it

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Structure, coded it, works, want to "upgrade" it

    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; 
                
           
      
    }
    Last edited by alvarito; 10-05-2011 at 10:55 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    The GNU C Library - Memory Allocation. Either ask how many entries the user is going to enter as a first step and then allocate an array that large, or use a linked list or binary tree or something and add structs one by one as they are entered.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    Ok thank you, I see. Out of your proposals some are complicated some are not that much, however it looks like only the complicated ones are the ones that provide a true solution. I ll study the link you have given, I think I have for one week learning in there, it looks quite complicated because the idea of the Linked lists is something that I did not visualize how that was to be implemented for what cases. It looks I have come across my own stuff.

    regards

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Another option is to allocate an array of some size to start with and fill it in. Keep track of how close you are to filling up the array you've allocated and when it is full, use realloc() to add more space to the array. That way you won't have to know how big the array needs to be at the start of reading data but you also don't have the complexity of linked lists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding how "operator char*()" works.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 28
    Last Post: 10-05-2008, 01:59 PM
  2. Not atoi, not sprintf, but "int coded into char*"
    By Mariano L Gappa in forum C Programming
    Replies: 5
    Last Post: 11-26-2005, 01:53 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread