Go Down

Topic: Serial.write(ptr,len) ->increment ptr while applying a function (Read 849 times) previous topic - next topic

diesel

Hello,

I was wondering if it is possible to apply a function to a char as it is being written with Serial.write. Something as follows:

char buffer[20]={2,3,4,5,6,7,8,9.....};
char ptr = &buffer[0];
char len = 20;

char * updatevalue(*char ptr){
uint8_t c = *ptr*2;
return &c;
}

Serial.write(updatevalue(ptr++),len);

Not sure if this is possible, can anybody help out?

Thanks,

-diesel



Nick Gammon

Not like that. You are returning a pointer to a local variable.

This is incredibly convoluted. What about a "for" loop that just steps through the array?

If you weren't submitting an entry in the "obsfuscated C competition" I would mark you down heavily for that code. It's almost incomprehensible. No offence. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Code: [Select]
char * updatevalue(*char ptr){
uint8_t c = *ptr*2;
return &c;
}

Did you bother even trying to compile this?

Nick Gammon

Let me try ...

Code: [Select]
char buffer[20]={2,3,4,5,6,7,8,9};
char ptr = &buffer[0];


Gives:

Code: [Select]
sketch_may03e:1: error: invalid conversion from 'char*' to 'char'
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

robtillaart

do you want to apply the function to the first element only or to all the elements?

your code returns the address of the local var c on the stack as the start of the array. That is a recipe for disaster

this is a refactored version that only changes the first char of the array. (stayed as close to your code as possible)
Code: [Select]

char buffer[20]={2,3,4,5,6,7,8,9.....};
char* ptr = &buffer[0];     //  Patched here
char len = 20;

char * updatevalue(*char ptr){
uint8_t c = *ptr*2;
return ptr;   // and here
}

Serial.write(updatevalue(ptr++),len);


all elements
Code: [Select]

for (int i=0; i<len; i++)
{
  char c = buffer[i] * 2;  // keep original content intact? otherwise use buffer[i] *= 2;  Serial.write(buffer[i];
  Serial.write(c);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

michael_x

After you've been bashed so hard:
Your original question "increment ptr while applying a function"
--> that's possible of course:

Code: [Select]
int get2Value(char* p) { return ( (*p) << 1) ; }

char buffer[20]={2,3,4,5,6,7,8,9};
char* ptr = buffer;     
char len = 10;
...
void setup()
{
  while (len-- != 0 )
  {
     Serial.print(get2Value(ptr++)); Serial.print(' ');  // prints "4 6 8 10 12 14 16 18 0 0 "
  }
}

This sample does not change the buffer content, but it increments ptr after it has been used to call a function.

Hope you get the other hints, too ;)

diesel

Well, the reason I brought it up is because I have seen some code where people create a class that inherits from print.h, they create their own function for write to execute a function on the chars. I thought this might be a way to get around that.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy