Problem passing array to function

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 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 = ASCIIHEX_to_DEC(buf[i*2]);        

   

    }

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 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]); 

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 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 = ASCIIHEX_to_DEC(buf + i*2);  

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes