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 CCS Technical Support

I need an explanation about problem with pointers

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



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

I need an explanation about problem with pointers
PostPosted: Sat Oct 18, 2014 2:48 am     Reply with quote

Greetings! I`m using MPlab v 8.91, CCS v5.025 and PIC18F67K22. This is my original code:
Code:

unsigned int value8bits;
unsigned int16 value16bits;

void Function(int16 *pointerToValue)
{
        //value8Bits is on address 010D
        //value16Bits is on address 010E
      *pointerToValue=10;
}

void main()
{
    int16 *pointer;
    value8bits=5;
    value16bits=200;
    pointer=&value8bits;
    Function(pointer);
    while(1);
}



After this code value16bits has 0 as value. Somehow
*pointerToValue=10;
changes value16bits. pointerToValue points to 010D - the correct address. value8bits has correct value 10.
When I changed the type of value8bits to int16 everything started working fine.
What`s this? Where is the problem? What am I missing?!
Is it because my pointer is int16 and it points to 2 bytes -> to value8bits and the one of value16bits?!
Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 18, 2014 9:05 am     Reply with quote

Quote:
What am I missing?!

You are missing your morning coffee.
You are using a 16-bit pointer to access an 8-bit variable.

Quote:
Is it because my pointer is int16 and it points to 2 bytes -> to value8bits and the one of value16bits?!

That is correct.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Oct 18, 2014 9:38 am     Reply with quote

Quote:

Is it because my pointer is int16 and it points to 2 bytes -> to value8bits and the one of value16bits?!

I wouldn't say 'correct' to this.

You are telling the compiler that the pointer points to an int16, and then pointing it 'at' an int8 value. When you update the value, it updates the int16 value pointed to, which also then includes the low byte of the next variable....

'value16bits', does not enter the equation, except as the value that incorrectly gets overwritten by the second byte of the int16 value.

int8 *pointer;

Will still have 'pointer' as a 16bit value, but the compiler then knows it points at an 8bit target.

The int16, in the pointer declaration, is not the size of the pointer, but the size of the variable it addresses.

int8 *pointer;
float *pointer;
int16 *pointer;
int32 *pointer;

All declare a pointer of exactly the same size, but if you write a value to each, for the first 4bytes are written to, the second and fourth, four bytes, and the third two bytes.

The 'size' in a pointer declaration, is not the size of the pointer, but what it addresses.
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