473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem in the C Structure code - pls help me.

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[i]->Name);
printf("\n Account No.: ");
gets(List[i]->Account_No);
printf("\n *****Address*****\n");
printf("\n Street: ");
gets(List[i]->Address.Street);
printf("\n City: ");
gets(List[i]->Address.City);
printf("\n State: ");
gets(List[i]->Address.State);
printf("\n Pin Code: ");
scanf("%d", &List[i]->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[i]->Name);
printf("\n Account No.: %s", List[i]->Account_No);
printf("\n *****Address***** \n");
printf("\n Street: %s", List[i]->Address.Street);
printf("\n City: %s", List[i]->Address.City);
printf("\n State: %s", List[i]->Address.State);
printf("\n Pin Code: %d", List[i]->Address.Pin);
}
}

Thanking you in advance.

Regards,
Vasu

Sep 13 '07 #1
3 1757

"Vasu" <va**************@yahoo.co.ukwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
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[i]->Name);
printf("\n Account No.: ");
gets(List[i]->Account_No);
printf("\n *****Address*****\n");
printf("\n Street: ");
gets(List[i]->Address.Street);
printf("\n City: ");
gets(List[i]->Address.City);
printf("\n State: ");
gets(List[i]->Address.State);
printf("\n Pin Code: ");
scanf("%d", &List[i]->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[i]->Name);
printf("\n Account No.: %s", List[i]->Account_No);
printf("\n *****Address***** \n");
printf("\n Street: %s", List[i]->Address.Street);
printf("\n City: %s", List[i]->Address.City);
printf("\n State: %s", List[i]->Address.State);
printf("\n Pin Code: %d", List[i]->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
Sep 13 '07 #2
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[i] ...]
gets(List[i]->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[i]->xxx" to
"List[i].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[i]->Account_No);
printf("\n *****Address*****\n");
printf("\n Street: ");
gets(List[i]->Address.Street);
printf("\n City: ");
gets(List[i]->Address.City);
printf("\n State: ");
gets(List[i]->Address.State);
printf("\n Pin Code: ");
scanf("%d", &List[i]->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[i] 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:Th*************@gmail.com>

Sep 13 '07 #3
On Sep 13, 11:50 am, Vasu <vasudevmukher...@yahoo.co.ukwrote:
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[i]->Name);
NEVER use gets(). NEVER. It is unsafe and it *will* introduce a
point of failure in your code. Use fgets() instead:

fgets(List[i]->Name, sizeof List[i]->Name);
printf("\n Account No.: ");
gets(List[i]->Account_No);
printf("\n *****Address*****\n");
printf("\n Street: ");
gets(List[i]->Address.Street);
printf("\n City: ");
gets(List[i]->Address.City);
printf("\n State: ");
gets(List[i]->Address.State);
printf("\n Pin Code: ");
scanf("%d", &List[i]->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[i]->Name);
printf("\n Account No.: %s", List[i]->Account_No);
printf("\n *****Address***** \n");
printf("\n Street: %s", List[i]->Address.Street);
printf("\n City: %s", List[i]->Address.City);
printf("\n State: %s", List[i]->Address.State);
printf("\n Pin Code: %d", List[i]->Address.Pin);

}
}

Thanking you in advance.

Regards,
Vasu

Sep 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
7
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures,...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
5
by: B. Chernick | last post by:
Is this a bug or just bad programming? I've never encountered this problem before. (Bare minimum sample form to illustrate.) I've also tried this with a class. Same result. The error is: For...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
7
by: edsunder | last post by:
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.