Problem in the C Structure code - pls help me.

Discussion in 'C Programming' started by Vasu, Sep 13, 2007.

  1. Vasu

    Vasu Guest

    Hi! Can anybody there help me in analysis of the following code, which
    is a structure of customer's details, asks user to fill in the no. of
    customers and then their details. When I put in no. of customer as 2
    and start filling in users details, in the detail of second customer
    till the name of State is OK as soon as I fill in the detail of State
    and press enter, it shows the field of PIN code but then suddenly it
    says something like "Encountered an error application needs to be
    closed......", I use digimars C Compiler to compile the application
    and I run the compiler on top of DOS shell of Windows XP. Code goes
    right here :
    /*Definition of Structure*/
    /*Struct.C*/

    #include<stdio.h>
    /*Structure declaration for address*/

    struct Addr
    {
    char Street[30];
    char City[30];
    char State[20];
    int Pin[5];
    };

    /*Nested Structure for Customer's Info*/

    struct Customer
    {
    char Name[20];
    char Account_No[6];

    struct Addr Address;
    };

    /*Now main fuction starts */
    void main()
    {
    struct Customer *List[100];
    int i, n;
    printf("\n Input number of records you want to process: ");
    scanf("%d", &n);
    fflush(stdin);

    /*Asking to fill in the customer' info*/
    printf("\n Input the information of customer \n");
    for(i=0; i<n; i++)
    {
    printf("\n Name: ");
    gets(List->Name);
    printf("\n Account No.: ");
    gets(List->Account_No);
    printf("\n *****Address*****\n");
    printf("\n Street: ");
    gets(List->Address.Street);
    printf("\n City: ");
    gets(List->Address.City);
    printf("\n State: ");
    gets(List->Address.State);
    printf("\n Pin Code: ");
    scanf("%d", &List->Address.Pin);
    fflush(stdin);
    }

    printf("\n List of the customer \n");
    printf("8888888888888888888888888\n");
    for(i=0; i<n; i++)
    {
    printf("\n Name: %s", List->Name);
    printf("\n Account No.: %s", List->Account_No);
    printf("\n *****Address***** \n");
    printf("\n Street: %s", List->Address.Street);
    printf("\n City: %s", List->Address.City);
    printf("\n State: %s", List->Address.State);
    printf("\n Pin Code: %d", List->Address.Pin);
    }
    }

    Thanking you in advance.

    Regards,
    Vasu
     
    Vasu, Sep 13, 2007
    #1
    1. Advertising

  2. "Vasu" <> wrote in message
    news:...
    > Hi! Can anybody there help me in analysis of the following code, which
    > is a structure of customer's details, asks user to fill in the no. of
    > customers and then their details. When I put in no. of customer as 2
    > and start filling in users details, in the detail of second customer
    > till the name of State is OK as soon as I fill in the detail of State
    > and press enter, it shows the field of PIN code but then suddenly it
    > says something like "Encountered an error application needs to be
    > closed......", I use digimars C Compiler to compile the application
    > and I run the compiler on top of DOS shell of Windows XP. Code goes
    > right here :
    > /*Definition of Structure*/
    > /*Struct.C*/
    >
    > #include<stdio.h>
    > /*Structure declaration for address*/
    >
    > struct Addr
    > {
    > char Street[30];
    > char City[30];
    > char State[20];
    > int Pin[5];
    > };
    >
    > /*Nested Structure for Customer's Info*/
    >
    > struct Customer
    > {
    > char Name[20];
    > char Account_No[6];
    >
    > struct Addr Address;
    > };
    >
    > /*Now main fuction starts */
    > void main()
    > {
    > struct Customer *List[100];
    > int i, n;
    > printf("\n Input number of records you want to process: ");
    > scanf("%d", &n);
    > fflush(stdin);
    >
    > /*Asking to fill in the customer' info*/
    > printf("\n Input the information of customer \n");
    > for(i=0; i<n; i++)
    > {
    > printf("\n Name: ");
    > gets(List->Name);
    > printf("\n Account No.: ");
    > gets(List->Account_No);
    > printf("\n *****Address*****\n");
    > printf("\n Street: ");
    > gets(List->Address.Street);
    > printf("\n City: ");
    > gets(List->Address.City);
    > printf("\n State: ");
    > gets(List->Address.State);
    > printf("\n Pin Code: ");
    > scanf("%d", &List->Address.Pin);
    > fflush(stdin);
    > }
    >
    > printf("\n List of the customer \n");
    > printf("8888888888888888888888888\n");
    > for(i=0; i<n; i++)
    > {
    > printf("\n Name: %s", List->Name);
    > printf("\n Account No.: %s", List->Account_No);
    > printf("\n *****Address***** \n");
    > printf("\n Street: %s", List->Address.Street);
    > printf("\n City: %s", List->Address.City);
    > printf("\n State: %s", List->Address.State);
    > printf("\n Pin Code: %d", List->Address.Pin);
    > }
    > }
    >


    1. main() returns an int.
    2. Don't use gets(). What happens if you enter a Name with 20 characters?
    3. Why is Pin an array?
    4. fflush() cannot be used on stdin.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing
     
    Fred Kleinschmidt, Sep 13, 2007
    #2
    1. Advertising

  3. Vasu wrote:
    [...]
    > and press enter, it shows the field of PIN code but then suddenly it
    > says something like "Encountered an error application needs to be

    [...]
    > void main()


    Note that others will point out that "void main()" is not a
    standard signature for main. However, it may be a valid
    extension for your particular compiler.

    > {
    > struct Customer *List[100];


    Here, you define List as an array of 100 pointers to "struct
    Customer". However, until you initialize these pointers, they
    are of indeterminate value, and using them is undefined
    behavior.

    [... snip code which does not initialize List ...]
    > gets(List->Name);


    You have now scribbled on some memory, somewhere in the computer.
    If you were lucky, it would have crashed right here. However,
    according to your description, it didn't crash until later. This
    is one possible outcome of undefined behavior.

    Based on the code, the simplest method of correcting this may
    be to simply change List to an array of struct Customer, rather
    than an array of pointers. Then change "List->xxx" to
    "List.xxx".

    Note, too, that you don't prevent the user from requesting more
    than 100 records. This can be handled by changing List from an
    array of pointers and instead use a simple pointer, which you
    initialize with malloc() to point to an array of "n" structs.

    > printf("\n Account No.: ");
    > gets(List->Account_No);
    > printf("\n *****Address*****\n");
    > printf("\n Street: ");
    > gets(List->Address.Street);
    > printf("\n City: ");
    > gets(List->Address.City);
    > printf("\n State: ");
    > gets(List->Address.State);
    > printf("\n Pin Code: ");
    > scanf("%d", &List->Address.Pin);


    All of these scribble on some unknown memory. (Not to
    mention the lack of buffer overrun protection on any of
    these calls, even if List were valid.)

    > fflush(stdin);


    This is undefined. It may do something on your particular
    implementation, however.

    [...]

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 13, 2007
    #3
  4. Vasu

    John Bode Guest

    On Sep 13, 11:50 am, Vasu <> wrote:
    > Hi! Can anybody there help me in analysis of the following code, which
    > is a structure of customer's details, asks user to fill in the no. of
    > customers and then their details. When I put in no. of customer as 2
    > and start filling in users details, in the detail of second customer
    > till the name of State is OK as soon as I fill in the detail of State
    > and press enter, it shows the field of PIN code but then suddenly it
    > says something like "Encountered an error application needs to be
    > closed......", I use digimars C Compiler to compile the application
    > and I run the compiler on top of DOS shell of Windows XP.


    Your code is crashing because you don't understand how pointers
    work.

    When you declare the List array, you are allocating space for 100
    pointers to struct Customer, but you haven't allocated the space for
    the actual Customer data. Since you declare the array at local
    scope and don't initialize the elements, each element contains some
    random garbage value that may or may not correspond to a valid,
    writable memory address. So as you start writing elements, you're
    potentially clobbering something important.

    There are several ways out of this problem. The simplest is to make
    List a static array of struct Customer, like so:

    struct Customer List[100];

    That means you'll use the '.' operator instead of the '->' for
    referencing struct members.

    Alternately, you can allocate the List array dynamically, like so:

    #include <stdlib.h>
    ...
    struct Customer *List;
    ...
    printf("\n Input number of records you want to process: ");
    scanf("%d", &n);
    List = malloc(n * sizeof *List);
    if (!List)
    {
    /* handle memory allocation error */
    }

    For the first pass I'd stick with the static array.

    Code goes
    > right here :
    > /*Definition of Structure*/
    > /*Struct.C*/
    >
    > #include<stdio.h>
    > /*Structure declaration for address*/
    >
    > struct Addr
    > {
    > char Street[30];
    > char City[30];
    > char State[20];
    > int Pin[5];
    >
    > };
    >
    > /*Nested Structure for Customer's Info*/
    >
    > struct Customer
    > {
    > char Name[20];
    > char Account_No[6];
    >
    > struct Addr Address;
    >
    > };
    >
    > /*Now main fuction starts */
    > void main()


    Some compilers claim to support void main(), but it's safest to define
    main as returning int:

    int main(void)
    > {
    > struct Customer *List[100];


    See discussion above.

    > int i, n;
    > printf("\n Input number of records you want to process: ");
    > scanf("%d", &n);
    > fflush(stdin);


    fflush() is not defined to work on input streams -- the operation
    isn't meaningful, and it may leave your program in a bad state.
    >
    > /*Asking to fill in the customer' info*/
    > printf("\n Input the information of customer \n");
    > for(i=0; i<n; i++)
    > {
    > printf("\n Name: ");
    > gets(List->Name);


    NEVER use gets(). NEVER. It is unsafe and it *will* introduce a
    point of failure in your code. Use fgets() instead:

    fgets(List->Name, sizeof List->Name);

    > printf("\n Account No.: ");
    > gets(List->Account_No);
    > printf("\n *****Address*****\n");
    > printf("\n Street: ");
    > gets(List->Address.Street);
    > printf("\n City: ");
    > gets(List->Address.City);
    > printf("\n State: ");
    > gets(List->Address.State);
    > printf("\n Pin Code: ");
    > scanf("%d", &List->Address.Pin);
    > fflush(stdin);
    >
    > }
    >
    > printf("\n List of the customer \n");
    > printf("8888888888888888888888888\n");
    > for(i=0; i<n; i++)
    > {
    > printf("\n Name: %s", List->Name);
    > printf("\n Account No.: %s", List->Account_No);
    > printf("\n *****Address***** \n");
    > printf("\n Street: %s", List->Address.Street);
    > printf("\n City: %s", List->Address.City);
    > printf("\n State: %s", List->Address.State);
    > printf("\n Pin Code: %d", List->Address.Pin);
    >
    > }
    > }
    >
    > Thanking you in advance.
    >
    > Regards,
    > Vasu
     
    John Bode, Sep 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Bond
    Replies:
    0
    Views:
    533
    James Bond
    Aug 3, 2004
  2. jollyroger

    Pls help me to validate some code!

    jollyroger, Aug 6, 2007, in forum: HTML
    Replies:
    4
    Views:
    360
    Andy Dingley
    Aug 6, 2007
  3. Dave Ello

    Help pls - need to NS-proof some code!

    Dave Ello, Nov 28, 2003, in forum: Javascript
    Replies:
    2
    Views:
    99
    Dave Ello
    Nov 28, 2003
  4. Manu Ashok

    pls help me run this code

    Manu Ashok, Apr 6, 2004, in forum: Javascript
    Replies:
    1
    Views:
    82
    Brian Genisio
    Apr 6, 2004
  5. Susanne West
    Replies:
    1
    Views:
    103
    smallpond
    Apr 21, 2008
Loading...

Share This Page