j0h

parse function

j0h
Jul 26th, 2021
695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include <stdlib.h> //contians atoi for string to int conversion
  4.  
  5. void parse(char *input, int *x, int *y, int *z){
  6.     // serial port sends data that looks like int, int, int
  7.      
  8.     char *p;
  9.     p = strtok(input, ",");
  10.     if(p){
  11.       *x=atoi(p);
  12.     }
  13.     p = strtok(NULL, ",");
  14.  
  15.     if(p){
  16.       *y=atoi(p);
  17.     }
  18.     p = strtok(NULL, ",");
  19.     if(p){
  20.       *z=atoi(p);
  21.     }    
  22.  
  23.     }
  24.  
  25.  
  26. int main(){
  27.  
  28.     char input[16] = "545,394,2";
  29.     int x=0,y=0,z=0;
  30.    
  31. parse(input, &x,&y,&z);
  32.     printf("X: %d\n", x);
  33.     printf("Y: %d\n", y);
  34.     printf("Z: %d\n", z);  
  35.     return 0;
  36. }
  37.  
RAW Paste Data