?
Solved

struct error

Posted on 2003-02-24
13
Medium Priority
?
298 Views
Last Modified: 2010-04-17
Hi, the following small codes compiles without error but when I run it, I have a Acess violation error....

Can u tell me what I'm doing wrong


Code :
--------------------------------

#include <stdio.h>
#include <conio.h>
#include <string.h>
# define MaxProducts 20

        int  CountP=0;
        unsigned long NProducts=0;

        struct Sales{
        unsigned long   ProdNumber;
        char NameProduit[25];
        int Quantity;
        unsigned long PriceUnit;
        unsigned long PriceTotal;
        };

        struct Info{
        struct Sales Products[5] ;
        }Clients[3];


main()
{


for(CountP=1; CountP<=MaxProducts; CountP++){
printf("Enter product # %d :\n",CountP);
fflush(stdin);
scanf("%lu",Clients[1].Products[CountP].ProdNumber);
NProducts =  Clients[1].Products[CountP].ProdNumber;
}//end for
 getch();

return 0;
} //end main

--------------------------------


plz keep in mind that I have a struct inside a struct and I don't want to add anymore variables

Thank You
0
Comment
Question by:C-begin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 3

Expert Comment

by:patelgokul
ID: 8014604
try this

#include <stdio.h>
#include <conio.h>
#include <string.h>
# define MaxProducts 20

       int  CountP=0;
       unsigned long NProducts=0;

       struct Sales{
       unsigned long   ProdNumber;
       char NameProduit[25];
       int Quantity;
       unsigned long PriceUnit;
       unsigned long PriceTotal;
   
       struct Info{
       struct Sales Products[5] ;
       }Clients[3];


main()
{


for(CountP=1; CountP<=MaxProducts; CountP++){
printf("Enter product # %d :\n",CountP);
fflush(stdin);
scanf("%lu",Clients[1].Products[CountP].ProdNumber);
NProducts =  Clients[1].Products[CountP].ProdNumber;
}//end for
getch();

return 0;
} //end main
0
 

Author Comment

by:C-begin
ID: 8014655
not working, you forgot to close your first struck
0
 
LVL 9

Accepted Solution

by:
bhagyesht earned 200 total points
ID: 8014664
for scanf u need the memnory where to allocate the values (pass by reference) so change ur line from

scanf("%lu",Clients[1].Products[CountP].ProdNumber);

to

scanf("%lu",&Clients[1].Products[CountP].ProdNumber);

the & will pass by reference

this will solve ur porblem
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8014678
error is here
>>>scanf("%lu",Clients[1].Products[CountP].ProdNumber);

change to this
scanf("%lu",&Clients[1].Products[CountP].ProdNumber);
            --
notice the small '&'
i guess this was typo .. just in case u have no idea why & is there .. then let us know ..
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8014686
hmm same time .. i wrote answer .. then opened another question .. so took some time to submit :P..
anyways  just a typo and nuthing much
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8015492
Replace your scanf statement with this

scanf("%lu",&Clients[1].Products[CountP].ProdNumber);

You forgot to give the & character.


Let me know about it.
0
 

Expert Comment

by:cindygirl
ID: 8053743
Is the array CLients[3] supposed to be included in your struct? It does not appear to be.
struct Info{
  struct Sales Products[5] ;
  Clients[3]
};
I don't know if the above will make a difference
0
 

Expert Comment

by:CleanupPing
ID: 9447528
C-begin:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 9448240
points to bhagyesht..
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9449100
thanks akshay
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 9449602
it was just my recommendation, it will take time for this to get accepted.
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9449634
I gets accepted. :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Introduction to Processes
Starting up a Project

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question