casting pointers?

General discussion on mikroC.
Post Reply
Author
Message
gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

casting pointers?

#1 Post by gambrose » 26 May 2005 14:22

how do i cast a pointer to a char to a pointer to a int?

Code: Select all

int GPS_Week;

void GPS_Time(char * TSIP_Packet)
{
    char msg[7];

    LCD_Cmd(Lcd_CLEAR);       // Clear display
    LCD_Out_Cp_Const("Week Number");

    TSIP_Packet += 4; // Ignore the first 4 bytes (GPS time of week, single)

    GPS_Week = * TSIP_Packet; // needs to be cast to an int

    IntToStr(GPS_Week, &msg);
    LCD_Out(2,1,&msg);
}
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: casting pointers?

#2 Post by pizon » 27 May 2005 10:18

You can do something like this:

Code: Select all

// PIC16F877A

char * pus1;
int *psi1, si2;

void main() {

  PORTB = 0;               // init ports
  TRISB = 0;
  PORTC = 0;
  TRISC = 0;

  pus1 = &PORTB;           // pus1 points to PORTB,
  *pus1 = 0xAA;            //   so PORTB is set to 0xAA
  Delay_ms(1000);

  psi1 = &si2;             // psi1 points to si2 integer
  *psi1 = *pus1;           //   and si2 is being set to PORTB (0x00AA)

  psi1 = pus1;             // psi1 now points to the same place as pus1, i.e. PORTB
  *psi1 = 0xAABB;          // *psi1 now becomes 0xAABB, meaning that PORTB will
                           //   become 0xBB, and the next byte (address 0x0007,
                           //   which is PORTC, will become 0xAA.
}//~!
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 27 May 2005 10:35

OK thanks i can see how that is working.

I will actually have to do it differently as i forgot microC stores it's values backwards, why are the numbers stored this way?
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#4 Post by pizon » 27 May 2005 10:55

gambrose wrote:I will actually have to do it differently as i forgot microC stores it's values backwards, why are the numbers stored this way?
I'm not quite sure what you mean
If you mean that multi-byte variables ((un)signed int, (un)signed long) are stored from the LSB up, it's a more-less standard way of doing it.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#5 Post by gambrose » 27 May 2005 11:01

I though it might be a standard way of doing it (i would have been surprised if you had just done it for a giggle).
I was just wondering about the reason for doing it this way?

As i have found that most data passed by streams is represented the way you read it MSB down.
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#6 Post by pizon » 28 May 2005 19:54

Which way is more convenient depends on what you intend to do with the data . If you want to directly display the data received, then it's better to have the MSB first. If, however, you want to do some calculus, then starting with the LSB is the way to go.
pizon

Post Reply

Return to “mikroC General”