CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pointers

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aodj



Joined: 20 Dec 2006
Posts: 40
Location: Reading, UK

View user's profile Send private message

Pointers
PostPosted: Thu May 31, 2007 3:53 am     Reply with quote

I'm currently dealing with some pointers and I've ended up with the following scenario:

Variable v1 is a signed int16, and contains -1 or 11111111 11111110.
Variable v2 is a signed int16 also.
Code:

signed int16 v1, v2;
v1 = -1;
v2 = &v1;

*v2 returns 255, or 00000000 11111110.

I can't work out why the pointer is being converted to an int8 and then cast back to an int16, losing the leading 8 bits.

Any ideas?
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Thu May 31, 2007 4:00 am     Reply with quote

Hello,

I do not think that a pointer can be signed ?

dro
_________________
in médio virtus
aodj



Joined: 20 Dec 2006
Posts: 40
Location: Reading, UK

View user's profile Send private message

PostPosted: Thu May 31, 2007 4:02 am     Reply with quote

Even if i use it as an int16, it still loses the leading 8 bits of the data.

What I think is happening, is the contents is being cast to an int8, and then recast to an int16.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Thu May 31, 2007 4:25 am     Reply with quote

Hello,

Witch microcontroler are you using ?
If you use a 14 bit parts, have you put the #device *=16 directive.

Code:

signed int16 v1;
int16 *v2 = &v1 ;

v1 = -1;



So *v2 is really a pointer.
v2 contain the addresses of v1.
*v2 give the value of v1;

I cannot verify that just now because i have no development board ready.

dro
_________________
in médio virtus
aodj



Joined: 20 Dec 2006
Posts: 40
Location: Reading, UK

View user's profile Send private message

PostPosted: Thu May 31, 2007 4:33 am     Reply with quote

inservi wrote:
Code:

signed int16 v1;
int16 *v2 = &v1 ;

v1 = -1;



So *v2 is really a pointer.
v2 contain the addresses of v1.
*v2 give the value of v1;

What you've written above should be right, but I'm finding that the result ends up losing the leading 8 bits, as detailed in my first post.

I'm using a 16F914, and version 4.016 of the compiler.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Thu May 31, 2007 7:53 am     Reply with quote

Hello,

I make some test with my version 4.039 and a 18F252.

with :
signed int16 v1;
int16 *v2 = &v1 ;


v1= -1 in binary: 11111111 11111111
*v2 = 65535 in binary 11111111 11111111 OK

v1 = -2 in binary : 11111111 11111110
*v2 = 65534 in binary : 11111111 11111110 OK

v1 = 2 in binary : 00000000 0000010
*v2 = 2 in binary : 00000000 0000010 OK


There is probably an error in the version 4.016 ?

dro
_________________
in médio virtus
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 31, 2007 11:04 am     Reply with quote

I compiled the test program shown below with PCM vs. 4.016 and
it works OK. I tested it with MPLAB simulator, with output directed
to 'UART1'. Here is the result, displayed in the Output window:
Quote:

-1
FFFF

Code:
#include <16F914.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT 
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
signed int16 v1;
signed int16 *v2;

v1 = -1;
v2 = &v1;

printf("%ld \n\r", *v2);
printf("%LX", *v2);

while(1);
}
Ttelmah
Guest







PostPosted: Thu May 31, 2007 2:41 pm     Reply with quote

I'd wonder if the fault is in the output. Remember, if you use:

printf("%d \n\r", *v2);

It'll print '255', since you are telling it the contents of'v' are an integer, not a long integer, and only the low byte will then be accessed..

Best Wishes
aodj



Joined: 20 Dec 2006
Posts: 40
Location: Reading, UK

View user's profile Send private message

PostPosted: Tue Jun 05, 2007 2:32 am     Reply with quote

After some more digging, and excessive use of the simulator, I've narrowed my problem down to one command, in so far as I can tell.

Code:
void fIncrement(signed int16 vPrimary, signed int16 vCPrimary)
{
   *vPrimary = *vCPrimary;
}

appears to pass only the lower 8 bits of the numbers. I'm using the following declaration:

Code:
fIncrement(&vOffset, &vCOffset);




The above image shows the bitpatterns I'm getting from the MPLAB simulator; note only the lower 8 bits are changing.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group