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. Initializing structure with pointers to char arrays - bug

Initializing structure with pointers to char arrays - bug

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancec++hardwaredata-structures
8 Posts 4 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    Could someone please help me to find my bug.
    This is C++ code "running" on Arduino.
    It compiles and runs ,but...
    I am using a struct to define / collect data which belong together.
    The bug is when I enter a character array (using pointer) into the structure variable, also a character array.
    The actual variable I am having problem with is ctextPrompts. I am sure there will be more, but this is the first bug.
    It is entered properly - verified by test code immediately after - see TOK here note.
    However, when I initialize next member of the struture cTextEntry - it wipes out the ctextPrompts.
    Initializing next structure member ctextEmulate puts ctextEmulate characters into ctextPrompts.

    This is my first attempt to utilize pointers and structure and I MUST have done something really stupid to cause this bug.

    I am using LCD to debug the code.

    Since the rest of the code works as expected I am pretty sure it is not a "memory" issue, I do verify the size of available RAM on initialization.
    I doubt it is a Arduino compiler issue, just plain wrong usage of pointers which I just do not see.

    I do apologize for the long code, but I feel it would help to see the debbuging attempts I have made so far.

    Thanks for your help.
    Cheers
    Vaclav

    PS While testing I am using both pointer (*) and indexing [0] with same results

    char *ctextEntry[] ={ " ", " "," " }; // emulating data char *ctextEmulate[] = { // emulate entries "14260123 ", // start frequency "14360000 ", // end frequency "10000 ", // step frequency "1 ", // step speed "1 ", // 0 single shot 1 repeat "Option" // }; // temporary not completed char *ctextPrompts[] ={ "Start frequency ","End frequency ","Step frequency ","Step speed ","Single / Repeat","Optional "}; typedef struct DataRecordTAG { char *cPrompts[]; // prompt text char *cEmulate[]; // emulate input char *cEntry[]; // response char cTEST; int iIndex; } DataRecord; DataRecord Record[DATA_STRUCT]; // variable int igRecordIndex = 0; // global access to records ....... void InitializeRecords(void) { // changed to global igRecordIndex igRecordIndex // igRecordIndex // initialize prompts and some test entries int iPromptIndex = 0; for

    C A R 3 Replies Last reply
    0
    • V Vaclav_

      Could someone please help me to find my bug.
      This is C++ code "running" on Arduino.
      It compiles and runs ,but...
      I am using a struct to define / collect data which belong together.
      The bug is when I enter a character array (using pointer) into the structure variable, also a character array.
      The actual variable I am having problem with is ctextPrompts. I am sure there will be more, but this is the first bug.
      It is entered properly - verified by test code immediately after - see TOK here note.
      However, when I initialize next member of the struture cTextEntry - it wipes out the ctextPrompts.
      Initializing next structure member ctextEmulate puts ctextEmulate characters into ctextPrompts.

      This is my first attempt to utilize pointers and structure and I MUST have done something really stupid to cause this bug.

      I am using LCD to debug the code.

      Since the rest of the code works as expected I am pretty sure it is not a "memory" issue, I do verify the size of available RAM on initialization.
      I doubt it is a Arduino compiler issue, just plain wrong usage of pointers which I just do not see.

      I do apologize for the long code, but I feel it would help to see the debbuging attempts I have made so far.

      Thanks for your help.
      Cheers
      Vaclav

      PS While testing I am using both pointer (*) and indexing [0] with same results

      char *ctextEntry[] ={ " ", " "," " }; // emulating data char *ctextEmulate[] = { // emulate entries "14260123 ", // start frequency "14360000 ", // end frequency "10000 ", // step frequency "1 ", // step speed "1 ", // 0 single shot 1 repeat "Option" // }; // temporary not completed char *ctextPrompts[] ={ "Start frequency ","End frequency ","Step frequency ","Step speed ","Single / Repeat","Optional "}; typedef struct DataRecordTAG { char *cPrompts[]; // prompt text char *cEmulate[]; // emulate input char *cEntry[]; // response char cTEST; int iIndex; } DataRecord; DataRecord Record[DATA_STRUCT]; // variable int igRecordIndex = 0; // global access to records ....... void InitializeRecords(void) { // changed to global igRecordIndex igRecordIndex // igRecordIndex // initialize prompts and some test entries int iPromptIndex = 0; for

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Quote:

      *Record[igRecordIndex].cPrompts = ctextPrompts[igRecordIndex];

      What are you doing with this line?

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      V 1 Reply Last reply
      0
      • V Vaclav_

        Could someone please help me to find my bug.
        This is C++ code "running" on Arduino.
        It compiles and runs ,but...
        I am using a struct to define / collect data which belong together.
        The bug is when I enter a character array (using pointer) into the structure variable, also a character array.
        The actual variable I am having problem with is ctextPrompts. I am sure there will be more, but this is the first bug.
        It is entered properly - verified by test code immediately after - see TOK here note.
        However, when I initialize next member of the struture cTextEntry - it wipes out the ctextPrompts.
        Initializing next structure member ctextEmulate puts ctextEmulate characters into ctextPrompts.

        This is my first attempt to utilize pointers and structure and I MUST have done something really stupid to cause this bug.

        I am using LCD to debug the code.

        Since the rest of the code works as expected I am pretty sure it is not a "memory" issue, I do verify the size of available RAM on initialization.
        I doubt it is a Arduino compiler issue, just plain wrong usage of pointers which I just do not see.

        I do apologize for the long code, but I feel it would help to see the debbuging attempts I have made so far.

        Thanks for your help.
        Cheers
        Vaclav

        PS While testing I am using both pointer (*) and indexing [0] with same results

        char *ctextEntry[] ={ " ", " "," " }; // emulating data char *ctextEmulate[] = { // emulate entries "14260123 ", // start frequency "14360000 ", // end frequency "10000 ", // step frequency "1 ", // step speed "1 ", // 0 single shot 1 repeat "Option" // }; // temporary not completed char *ctextPrompts[] ={ "Start frequency ","End frequency ","Step frequency ","Step speed ","Single / Repeat","Optional "}; typedef struct DataRecordTAG { char *cPrompts[]; // prompt text char *cEmulate[]; // emulate input char *cEntry[]; // response char cTEST; int iIndex; } DataRecord; DataRecord Record[DATA_STRUCT]; // variable int igRecordIndex = 0; // global access to records ....... void InitializeRecords(void) { // changed to global igRecordIndex igRecordIndex // igRecordIndex // initialize prompts and some test entries int iPromptIndex = 0; for

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

        There maybe be several bugs, but I believe you should remove the []:

        ypedef struct DataRecordTAG
        {
        char *cPrompts; // prompt text
        char *cEmulate; // emulate input
        char *cEntry; // response

        char cTEST;
        int iIndex;
        }

        V 3 Replies Last reply
        0
        • C CPallini

          Quote:

          *Record[igRecordIndex].cPrompts = ctextPrompts[igRecordIndex];

          What are you doing with this line?

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          V Offline
          V Offline
          Vaclav_
          wrote on last edited by
          #4

          Initializing the struct member cPrompts (char array) from ctextPrompts (char array). According to next verification code it works. But than things go crazy as described.

          1 Reply Last reply
          0
          • A ahmad_ali

            There maybe be several bugs, but I believe you should remove the []:

            ypedef struct DataRecordTAG
            {
            char *cPrompts; // prompt text
            char *cEmulate; // emulate input
            char *cEntry; // response

            char cTEST;
            int iIndex;
            }

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            I think what I am doing is assigning pointers in the structure to point to an array. Is that legal / right? Or do I need to physically copy the array to the structure? That will probably wipe out the memory for good. I am sure lost in this . Maybe I need to not use the structure at all.

            1 Reply Last reply
            0
            • A ahmad_ali

              There maybe be several bugs, but I believe you should remove the []:

              ypedef struct DataRecordTAG
              {
              char *cPrompts; // prompt text
              char *cEmulate; // emulate input
              char *cEntry; // response

              char cTEST;
              int iIndex;
              }

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              Now that does not "copy" the entire array into the structure. But I think I need to work on making the structure pointer equal the array pointer.

              1 Reply Last reply
              0
              • A ahmad_ali

                There maybe be several bugs, but I believe you should remove the []:

                ypedef struct DataRecordTAG
                {
                char *cPrompts; // prompt text
                char *cEmulate; // emulate input
                char *cEntry; // response

                char cTEST;
                int iIndex;
                }

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                I did remove the array and so far it is working. But I need to check the rest of the code . Thanks Vaclav So basically if read my code in English = assigning a char pointer to point to pointer to array of characters. What I did was wrongly assign a char array pointer to point to array of characters. Thanks to both of you guys to nudge me the correct this. Vaclav

                1 Reply Last reply
                0
                • V Vaclav_

                  Could someone please help me to find my bug.
                  This is C++ code "running" on Arduino.
                  It compiles and runs ,but...
                  I am using a struct to define / collect data which belong together.
                  The bug is when I enter a character array (using pointer) into the structure variable, also a character array.
                  The actual variable I am having problem with is ctextPrompts. I am sure there will be more, but this is the first bug.
                  It is entered properly - verified by test code immediately after - see TOK here note.
                  However, when I initialize next member of the struture cTextEntry - it wipes out the ctextPrompts.
                  Initializing next structure member ctextEmulate puts ctextEmulate characters into ctextPrompts.

                  This is my first attempt to utilize pointers and structure and I MUST have done something really stupid to cause this bug.

                  I am using LCD to debug the code.

                  Since the rest of the code works as expected I am pretty sure it is not a "memory" issue, I do verify the size of available RAM on initialization.
                  I doubt it is a Arduino compiler issue, just plain wrong usage of pointers which I just do not see.

                  I do apologize for the long code, but I feel it would help to see the debbuging attempts I have made so far.

                  Thanks for your help.
                  Cheers
                  Vaclav

                  PS While testing I am using both pointer (*) and indexing [0] with same results

                  char *ctextEntry[] ={ " ", " "," " }; // emulating data char *ctextEmulate[] = { // emulate entries "14260123 ", // start frequency "14360000 ", // end frequency "10000 ", // step frequency "1 ", // step speed "1 ", // 0 single shot 1 repeat "Option" // }; // temporary not completed char *ctextPrompts[] ={ "Start frequency ","End frequency ","Step frequency ","Step speed ","Single / Repeat","Optional "}; typedef struct DataRecordTAG { char *cPrompts[]; // prompt text char *cEmulate[]; // emulate input char *cEntry[]; // response char cTEST; int iIndex; } DataRecord; DataRecord Record[DATA_STRUCT]; // variable int igRecordIndex = 0; // global access to records ....... void InitializeRecords(void) { // changed to global igRecordIndex igRecordIndex // igRecordIndex // initialize prompts and some test entries int iPromptIndex = 0; for

                  R Offline
                  R Offline
                  Raushank03
                  wrote on last edited by
                  #8

                  To get your Answer you can visit on http://techgurulab.com/

                  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