Connecting Tech Pros Worldwide Forums | Help | Site Map

access violation error while trying to memset() the typedef element of the struct

Newbie
 
Join Date: Nov 2009
Posts: 1
#1: 3 Weeks Ago
Hi All,
I have a typedef element defined inside the struct which i am trying to assign value using the memset and strcpy function.I am able to compile but while running it give me access violation error.Here is my code
Expand|Select|Wrap|Line Numbers
  1. **********
  2. #ifndef DEFINE_SAMPLETYPES
  3. #define DEFINE_SAMPLETYPES
  4.  
  5. typedef char MANUFACTURER_PART_NUMBER[48];
  6.  
  7. #endif
  8.  
  9. *******************************************
  10. #ifndef DEFINE_SAMPLE_WKSP
  11. #define DEFINE_SAMPLE_WKSP
  12. #include "SAMPLETYPEDEF.H"
  13. typedef struct{
  14.  
  15. MANUFACTURER_PART_NUMBER manufacturer_part_number;
  16.  
  17. }IN_SAMPLE_WKSP;
  18.  
  19. #endif
  20. ****************************
  21. #include "SAMPLE_WKSP.H"
  22.  
  23.  IN_SAMPLE_WKSP *po;
  24.  
  25.  main()
  26.  {
  27.  
  28. memset(&(po)->manufacturer_part_number,' ',48);
  29. strcpy(&(po)->manufacturer_part_number,"hello");
  30.  
  31. }
  32.  
can you guide me where i am doing mistake.Is this correct way of accessing the typedef element in a struct.

Thanks for the Help

ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 390
#2: 3 Weeks Ago

re: access violation error while trying to memset() the typedef element of the struct


Quote:

Originally Posted by bamsi View Post

Hi All,
I have a typedef element defined inside the struct which i am trying to assign value using the memset and strcpy function.I am able to compile but while running it give me access violation error.Here is my code
**********
#ifndef DEFINE_SAMPLETYPES
#define DEFINE_SAMPLETYPES

typedef char MANUFACTURER_PART_NUMBER[48];

#endif

*******************************************
#ifndef DEFINE_SAMPLE_WKSP
#define DEFINE_SAMPLE_WKSP
#include "SAMPLETYPEDEF.H"
typedef struct{

MANUFACTURER_PART_NUMBER manufacturer_part_number;

}IN_SAMPLE_WKSP;

#endif
****************************
#include "SAMPLE_WKSP.H"

IN_SAMPLE_WKSP *po;

main()
{

memset(&(po)->manufacturer_part_number,' ',48);
strcpy(&(po)->manufacturer_part_number,"hello");

}

can you guide me where i am doing mistake.Is this correct way of accessing the typedef element in a struct.

Thanks for the Help

According to my knowledge,
you ll have to first allocate the memory to IN_SAMPLE_WKSP *po;
then do your memset and other logic.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: 3 Weeks Ago

re: access violation error while trying to memset() the typedef element of the struct


Another way to put it is that po is an uninitialized pointer. You need to initialize it to point at an IN_SAMPLE_WKSP.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#4: 3 Weeks Ago

re: access violation error while trying to memset() the typedef element of the struct


Quote:

Originally Posted by bamsi View Post

Expand|Select|Wrap|Line Numbers
  1. memset(&(po)->manufacturer_part_number,' ',48);
  2. strcpy(&(po)->manufacturer_part_number,"hello");
  3.  

Even if you initialise po correctly these lines are not correct the & is unnecessary. This effects the type, rather than the value, of the pointer but most good compilers would issue a warning for this.
Reply