Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

struct error

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
C-begin
Asked:
C-begin
1 Solution
 
patelgokulCommented:
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
 
C-beginAuthor Commented:
not working, you forgot to close your first struck
0
 
bhagyeshtCommented:
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
Technology Partners: 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!

 
akshayxxCommented:
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
 
akshayxxCommented:
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
 
rajeev_devinCommented:
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
 
cindygirlCommented:
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
 
CleanupPingCommented:
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
 
akshayxxCommented:
points to bhagyesht..
0
 
bhagyeshtCommented:
thanks akshay
0
 
akshayxxCommented:
it was just my recommendation, it will take time for this to get accepted.
0
 
bhagyeshtCommented:
I gets accepted. :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now