passing fixed data and variables to function using pointers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

passing fixed data and variables to function using pointers

3,557 Views
stevec
Contributor III
I have a number of predefined data strings and also a variable area which I wish to be accessed by a function which sends  the data bytes to an output routine.
I have assigned the fixed strings as
    const unsigned char string1[] = "This is string 1";
    const unsigned char string2[] = "And this is another string";

    etc
the variable is in
    unsigned char temp_store[256];

I am passing reference using
    size = sizeof(store1) -1;
    send_data_to_output(store1, size);    
    OR
    size = sizeof(temp_store) - 1;         //assume a NUL added to end of data as terminator
    send_data_to_output(temp_store, size)


the sending routine is defines as
    void send_data_to_output(unsigned char *pointer, unsigned char data_size)

 When ' temp_store' is passed it compiles ok but when I try to pass 'string1' for example it gives me an "Indirection to different types" error. I can see why its doing it as one is const and the other isnt. How do I get my routine to work with both constant and variable data? without having first to transfer the const data into a variable?

Steve
Labels (1)
0 Kudos
Reply
5 Replies

669 Views
CompilerGuru
NXP Employee
NXP Employee
The ColdFire compiler and other MW compilers are more strict than the 8/16 bit compilers when implicitely loosing the constness. Regardless, I would always code const correct in the first place so the 8/16 bit compiler do not issue a warning and the other MW compiler have no reason to issue an error.

With the assumption that send_data_to_output is not modifying the data to be sent, just add the const to the
argument list:

extern void send_data_to_output(const unsigned char *pointer, unsigned char data_size);

It's not a problem for the compiler to add a const implicitely (in the first indirection), just to loose the const is the bad thing.

If the data_size parameter is necessary probably depends on if this function is only used to send out zero terminated strings, or if it is used to send binary data too.

Daniel

PS: Please always mention the architecture, compiler version, copy paste the error message,...

PS: adding a const to the argument usually propagates further in the code. But most of the time it should not be necessary to add a cast to shutup the compiler. Just when you get to some 3'rd party sources you don't want to adapt.

Yet another PS: probably it should be "size = strlen(temp_store)" and not sizeof()-1 :smileywink:
0 Kudos
Reply

669 Views
stevec
Contributor III
I was wanting to use the function to output from a const list OR from a RAM based variable. Won't the compiler throw a wobbly if I try to pass a reference to a variable (unsigned char) rather than a const if I add the const to the argument list.

I shall not be outputting binary so I note your comments on terminating conditions. I will have another similar routine though that will output binary so will have to include a length byte somewhere.
0 Kudos
Reply

669 Views
bigmac
Specialist III
Hello Steve,
 
Another approach, that seems to work at least on 8-bit MCUs, without error or warning, is to use a cast when referencing constant string data.
send_data_to_output((unsigned char *)string1, ... );
 
And for a RAM based array, a cast would not be required.
 
To handle both null terminated string data, and binary data, in the one function might be done more easily by simply using a length value of zero for string data, and specifying the actual length for binary data.
 
void send_data_to_output (unsigned char *pointer, unsigned char data_size)
{
   byte i;
  
   if (data_size) {
      for (i = 0; i < data_size; i++)
         send_byte (*pointer++);
   }
   else {
      while (*pointer)
         send_byte (*pointer++);
   }
}
 
Regards,
Mac
 
0 Kudos
Reply

669 Views
CompilerGuru
NXP Employee
NXP Employee
Just add the const to the declaration of send_data_to_output,
and both the const and non const arrays can be passed with no cast (or warning)

>void send_data_to_output(const unsigned char *pointer,
>                          unsigned char data_size)


Daniel

BTW, while it will just work fine, I personally do not like magicargument values like that "data_size == 0" does mean "print the other argument as a zero terminated string".
For me sending a zero terminated string and sending n-char's are separate things to do and should have their own separate function.
For unification, the print zero terminated string can easely be mapped with a strlen to the print n-char's.

0 Kudos
Reply

669 Views
bigmac
Specialist III
Hello Steve,
 
When I compiled similar code, the compiler issued a warning (C1285), rather than an error, so the compile and link process actually successfully completed.  When run in the simulator, the code seemed to give the required result, in spite of the warning.
 
A further comment - for string data, why do you need the 'data_size' variable?  A potentially simpler alterative would be to output each character until a null byte is reached.
 
Regards,
Mac
 


Message Edited by bigmac on 2007-08-03 02:03 AM
0 Kudos
Reply