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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bamsi
    New Member
    • Nov 2009
    • 1

    #1

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

    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
    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
    Last edited by Banfa; Nov 4 '09, 02:05 PM. Reason: Added [Code] ... [/code] tags
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Originally posted by bamsi
    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_SAMPLETY PES
    #define DEFINE_SAMPLETY PES

    typedef char MANUFACTURER_PA RT_NUMBER[48];

    #endif

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

    MANUFACTURER_PA RT_NUMBER manufacturer_pa rt_number;

    }IN_SAMPLE_WKSP ;

    #endif
    *************** *************
    #include "SAMPLE_WKS P.H"

    IN_SAMPLE_WKSP *po;

    main()
    {

    memset(&(po)->manufacturer_p art_number,' ',48);
    strcpy(&(po)->manufacturer_p art_number,"hel lo");

    }

    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.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Another way to put it is that po is an uninitialized pointer. You need to initialize it to point at an IN_SAMPLE_WKSP.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by bamsi
        Code:
        memset(&(po)->manufacturer_part_number,' ',48);
        strcpy(&(po)->manufacturer_part_number,"hello");
        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.

        Comment

        Working...