5 Replies Latest reply on Dec 2, 2014 2:38 AM by JoMe_264151

    Problem passing array to function

    DaHu_285096

       I am having trouble casting and array to pass to a function. In the code below I am wanting to pass an array of ASCII HEX characters to a function that generates the Decimal value for each pair of characters.

         

      The line putting the rssult int the RawData array is coming up with error "Passing argument 1 of ASCIIHEX_to_DEC make pointer to interger without a cast.

         

      I have tried several approaches to modify the type being passed but cannot get figure out what I need to do to fix it.

         

      Thanks

         

       

         

      uint8 ASCIIHEX_to_DEC(char * ascii)

         

      {

         

          uint8 decval;

         

          

         

          if(ascii[0] >= 'A') decval = ((uint8) ascii[0] - 55) << 4; else decval = (uint8) ascii[0] << 4;

         

          if(ascii[1] >= 'A') decval +=  (uint8) ascii[1] - 55; else decval += (uint8) ascii[1];

         

          

         

          return decval;

         

      }

         

       

         

      //[MESSAGE_TYPE] [MESSAGE_TYPE] [DATA_LENGTH] [DATA_LENGTH] [DATA] [DATA] ... [DATA] [CRC] [CRC] [CRC] [CRC] [FOOTER_FLAG (0x7D)]

         

      void ProcessFrame(char * buf, uint8 packetsize)  

         

      {

         

          uint8 RawData[30];

         

          uint8 command;

         

          uint8 i;

         

          

         

          for(i= 0 ; i < (packetsize/2); i++)

         

          {

         

            RawData[i] = ASCIIHEX_to_DEC(buf[i*2]);        

         

          }

        • 1. Re: Problem passing array to function
          JoMe_264151

          buf[i*2] is a char, your function requires a pointer to the char, so use &buf[i*2].

             

          There can be a pitfall later: Depending on the compiler and the environment a char is not necessarily 8 bits wide, so I would suggest you to use uint8 for byte-buffers for the next projects.

             

           

             

          Happy coding

             

          Bob

          • 2. Re: Problem passing array to function
            DaHu_285096

             Thanks Bob,

               

            I would prefer to use uint8 for buffers. But, I had a problem with strstr and other string functions not likeing working with uint8.

               

            It seemed that either way I had to cast these things all the time.

               

            For some reason I got away with it with PIC compiler. All my buffers were unsigned char and had no issue with strstr or passing to other functions. 

               

             

               

            With your example, could I also cast like below? I need to pass the buffer reference to every second byte.

               

            Process_Packet( (char *) buf[2*i]); 

            • 3. Re: Problem passing array to function
              DaHu_285096

               Sorry, Made a silly mistake in my call, I usually do it like this... buf points to address of buf[0] and I add an offset. Must have been in dream world today.

                 

               

                 

              RawData[i] = ASCIIHEX_to_DEC(buf + i*2);  

              • 4. Re: Problem passing array to function
                JoMe_264151

                Process_Packet( (char *) buf[2*i]); 

                   

                That will probably not work! Compilers are stupid programs and they obey to every command literally and mostly from left to rigth, but keep in mind: mostly.

                   

                So your statement reads: take as a pointer to a char the value to be found in buf[2*i] and that again is not what you want

                   

                (char *)(&buf[2*i]) should work. did you already try buf+2*i which should work, too.

                   

                 

                   

                Bob

                • 5. Re: Problem passing array to function
                  JoMe_264151

                  That was a post-crossover, fine to see that we both agree with the compiler...

                     

                   

                     

                  Bob