Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. string copy problem in structure variable

string copy problem in structure variable

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitmistry_petlad
    wrote on last edited by
    #1

    Hi! Need help to copy string in the structure variable . following is my structure, struct argument_list { WCHAR *pszInFile; WCHAR *pszOutFil; :confused: what should i have used here string or char *?:confused: char* Host; // Initially I had used string but strcpy function does not support to copy the string char* UserID; char* InitPackageRequest; char* ScriptFile; }; I have made this structure for the thread. to pass the parameter. for that i have used the following. struct argument_list *l; l=(argument_list *)malloc(sizeof(argument_list)); `is lstrcpy require here?` l->pszInFile=bufferwithpath; l->pszOutFil=w_Output; l->_ProtectSet=_ProtectSet; l->hList=hList; //l->Host(HOST); l->Host=NULL; strcpy(l->Host,HOST); //got error here l->hwndEncrypt=hwndEncrypt; l->hWndinoutfiledir=hWndinoutfiledir; l->hwndParent=hDlg; l->InitPackageRequest=NULL; strcpy(l->InitPackageRequest,sINIT_PACKAGE_RESPONSE.c_str()); l->Port=PORT; l->ScriptFile=NULL; strcpy(l->ScriptFile,COMMUNICATOR_SCRIPT_FILE_PATH); l->UserID=NULL; strcpy(l->UserID,UserID.c_str());

    "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

    H A 2 Replies Last reply
    0
    • A amitmistry_petlad

      Hi! Need help to copy string in the structure variable . following is my structure, struct argument_list { WCHAR *pszInFile; WCHAR *pszOutFil; :confused: what should i have used here string or char *?:confused: char* Host; // Initially I had used string but strcpy function does not support to copy the string char* UserID; char* InitPackageRequest; char* ScriptFile; }; I have made this structure for the thread. to pass the parameter. for that i have used the following. struct argument_list *l; l=(argument_list *)malloc(sizeof(argument_list)); `is lstrcpy require here?` l->pszInFile=bufferwithpath; l->pszOutFil=w_Output; l->_ProtectSet=_ProtectSet; l->hList=hList; //l->Host(HOST); l->Host=NULL; strcpy(l->Host,HOST); //got error here l->hwndEncrypt=hwndEncrypt; l->hWndinoutfiledir=hWndinoutfiledir; l->hwndParent=hDlg; l->InitPackageRequest=NULL; strcpy(l->InitPackageRequest,sINIT_PACKAGE_RESPONSE.c_str()); l->Port=PORT; l->ScriptFile=NULL; strcpy(l->ScriptFile,COMMUNICATOR_SCRIPT_FILE_PATH); l->UserID=NULL; strcpy(l->UserID,UserID.c_str());

      "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

      H Offline
      H Offline
      Hans Dietrich
      wrote on last edited by
      #2

      amitmistry_petlad wrote:

      strcpy(l->Host,HOST); //got error here

      You got an error because there is no memory associated with the 'Host' member - it's just a pointer. You must either: 1) allocate memory before the strcpy - for example, l->Host = malloc(...); OR you can change the struct to a specific size for the 'Host' member - for example, char Host[100]. You seem to be confused about the difference between a char* pointer (char *s), and an array of char's (s[100]). You can use either one in the strcpy() function, but if you want to use a pointer, be sure you know where the memory has been allocated.

      A 1 Reply Last reply
      0
      • H Hans Dietrich

        amitmistry_petlad wrote:

        strcpy(l->Host,HOST); //got error here

        You got an error because there is no memory associated with the 'Host' member - it's just a pointer. You must either: 1) allocate memory before the strcpy - for example, l->Host = malloc(...); OR you can change the struct to a specific size for the 'Host' member - for example, char Host[100]. You seem to be confused about the difference between a char* pointer (char *s), and an array of char's (s[100]). You can use either one in the strcpy() function, but if you want to use a pointer, be sure you know where the memory has been allocated.

        A Offline
        A Offline
        amitmistry_petlad
        wrote on last edited by
        #3

        Thank you very much sir ! :) :rose:

        "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

        1 Reply Last reply
        0
        • A amitmistry_petlad

          Hi! Need help to copy string in the structure variable . following is my structure, struct argument_list { WCHAR *pszInFile; WCHAR *pszOutFil; :confused: what should i have used here string or char *?:confused: char* Host; // Initially I had used string but strcpy function does not support to copy the string char* UserID; char* InitPackageRequest; char* ScriptFile; }; I have made this structure for the thread. to pass the parameter. for that i have used the following. struct argument_list *l; l=(argument_list *)malloc(sizeof(argument_list)); `is lstrcpy require here?` l->pszInFile=bufferwithpath; l->pszOutFil=w_Output; l->_ProtectSet=_ProtectSet; l->hList=hList; //l->Host(HOST); l->Host=NULL; strcpy(l->Host,HOST); //got error here l->hwndEncrypt=hwndEncrypt; l->hWndinoutfiledir=hWndinoutfiledir; l->hwndParent=hDlg; l->InitPackageRequest=NULL; strcpy(l->InitPackageRequest,sINIT_PACKAGE_RESPONSE.c_str()); l->Port=PORT; l->ScriptFile=NULL; strcpy(l->ScriptFile,COMMUNICATOR_SCRIPT_FILE_PATH); l->UserID=NULL; strcpy(l->UserID,UserID.c_str());

          "Success lies not in the result , But in the efforts !!!!!" Amit Mistry - petlad -Gujarat-India

          A Offline
          A Offline
          amistry_petlad
          wrote on last edited by
          #4

          amit here

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups