Problem in the C Structure code - pls help me.

V

Vasu

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
 
F

Fred Kleinschmidt

Vasu said:
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.
 
K

Kenneth Brody

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:[email protected]>
 
J

John Bode

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top