Pointer subtraction problem, source boost pic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Salisbury

    Pointer subtraction problem, source boost pic

    Hi,

    Does anyone have a good page and some exercises on the use of pointers?
    The compiler is Source Boost and the target is an 18f picr. I am trying
    to find out why the code below won't compile. At line 244 I get invalid
    operand and failed to generate expression.

    Thanks


    char get_credit (void)
    {
    char test_string[]="Your balance is £4.38.";
    //char *CUSD = "AT+CUSD=1,*#10 #";
    char *pnd_point;
    char *dec_point;
    int length;
    char cb [5];
    //char pnd='£';
    //char dec='.';
    int credit_pnd;
    int credit_pence;
    //puts ("AT+CUSD=1,*#1 0#");
    //gsm_gets (rx_array);
    strcpy (rx_array,test_ string);
    pnd_point =strchr (rx_array,'£');
    dec_point =strchr (rx_array,0x2E) ;
    length =(pnd_point - dec_point); // <-----line 244
    strncpy (cb,(pnd_point+ 1),(length));
    credit_pnd = atoi (cb);
    strncpy (cb,(dec_point+ 1),2);
    credit_pence = atoi (cb);

    if (credit_pnd >5)
    {
    portb.0 = 1;
    portb.1 = 0;
    }
    else
    {
    portb.0 = 0;
    portb.1 = 1;
    }
    }

  • user923005

    #2
    Re: Pointer subtraction problem, source boost pic

    On Apr 18, 3:44 pm, James Salisbury
    <webmail_th...@ whatsthis.salis bury.org.ukwrot e:
    Hi,
    >
    Does anyone have a good page and some exercises on the use of pointers?
    The compiler is Source Boost and the target is an 18f picr. I am trying
    to find out why the code below won't compile. At line 244 I get invalid
    operand and failed to generate expression.
    >
    Thanks
    >
    char get_credit (void)
    {
    char test_string[]="Your balance is £4.38.";
    //char *CUSD = "AT+CUSD=1,*#10 #";
    char *pnd_point;
    char *dec_point;
    int length;
    char cb [5];
    //char pnd='£';
    //char dec='.';
    int credit_pnd;
    int credit_pence;
    //puts ("AT+CUSD=1,*#1 0#");
    //gsm_gets (rx_array);
    strcpy (rx_array,test_ string);
    pnd_point =strchr (rx_array,'£');
    dec_point =strchr (rx_array,0x2E) ;
    length =(pnd_point - dec_point); //   <-----line 244
    strncpy (cb,(pnd_point+ 1),(length));
    credit_pnd = atoi (cb);
    strncpy (cb,(dec_point+ 1),2);
    credit_pence = atoi (cb);
    >
    if (credit_pnd >5)
    {
    portb.0 = 1;
    portb.1 = 0;}
    >
    else
    {
    portb.0 = 0;
    portb.1 = 1;
    >
    >
    >
    }
    }- Hide quoted text -
    >
    - Show quoted text -
    #include <string.h>
    #include <stdlib.h>
    extern char *rx_array;

    void get_credit(void )
    {
    char test_string[] = "Your balance is £4.38.";
    char *pnd_point;
    char *dec_point;
    int length;
    char cb[5];
    int credit_pnd;
    int credit_pence;
    strcpy(rx_array , test_string);
    pnd_point = strchr(rx_array , '£');
    if (pnd_point) {
    dec_point = strchr(rx_array , 0x2E);
    if (dec_point) {
    length = (pnd_point - dec_point);

    strncpy(cb, (pnd_point + 1), (length));
    credit_pnd = atoi(cb);
    strncpy(cb, (dec_point + 1), 2);
    credit_pence = atoi(cb);
    if (credit_pnd 5) {
    /* These assignments look to be the problem.
    * It looks like some kind of extension for
    * embedded systems to read and write
    * directly from and to port numbers. C has
    * no provision for this. I guess you will
    * have to look at you embedded compiler
    * documentation to find out how to do it.
    * Clearly, you will also need to define portb,
    * probably via the inclusion of a header file.
    */

    portb.0 = 1;
    portb.1 = 0;
    } else {
    portb.0 = 0;
    portb.1 = 1;
    }
    }
    }
    }

    I guess you are better off asking your question in
    news:comp.arch. embedded

    Comment

    • Barry Schwarz

      #3
      Re: Pointer subtraction problem, source boost pic

      On Fri, 18 Apr 2008 23:44:32 +0100, James Salisbury
      <webmail_think@ whatsthis.salis bury.org.ukwrot e:
      >Hi,
      >
      >Does anyone have a good page and some exercises on the use of pointers?
      >The compiler is Source Boost and the target is an 18f picr. I am trying
      >to find out why the code below won't compile. At line 244 I get invalid
      >operand and failed to generate expression.
      Do you get the error at run-time or at compile time?
      >
      >Thanks
      >
      >
      >char get_credit (void)
      >{
      >char test_string[]="Your balance is £4.38.";
      >//char *CUSD = "AT+CUSD=1,*#10 #";
      >char *pnd_point;
      >char *dec_point;
      >int length;
      >char cb [5];
      >//char pnd='£';
      >//char dec='.';
      >int credit_pnd;
      >int credit_pence;
      >//puts ("AT+CUSD=1,*#1 0#");
      >//gsm_gets (rx_array);
      >strcpy (rx_array,test_ string);
      Where is rx_array defined?
      >pnd_point =strchr (rx_array,'£');
      >dec_point =strchr (rx_array,0x2E) ;
      Is there a reason you didn't use '.'?
      >length =(pnd_point - dec_point); // <-----line 244
      My compiler does not generate any diagnostic for this line.
      >strncpy (cb,(pnd_point+ 1),(length));
      >credit_pnd = atoi (cb);
      >strncpy (cb,(dec_point+ 1),2);
      >credit_pence = atoi (cb);
      >
      >if (credit_pnd >5)
      >{
      >portb.0 = 1;
      >portb.1 = 0;
      What kind of objects are these?
      >}
      >else
      >{
      >portb.0 = 0;
      >portb.1 = 1;
      >}
      Where is your return statement.
      >}

      Remove del for email

      Comment

      Working...