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

Void pointer to different variable types.

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



Joined: 22 Dec 2003
Posts: 109
Location: West Sussex, UK

View user's profile Send private message Send e-mail Visit poster's website

Void pointer to different variable types.
PostPosted: Fri Oct 20, 2006 10:00 am     Reply with quote

Hi All,
I'm having trouble using a pointer to change different variable types, the code below (cut down from 30k's worth) shows the problem.
It works with 8 bit variables but not 16 (not shown) or 32 bit.
I'm probably doing something very silly, please put me out of my misery.

Thanks,

Jim Hearne.

Code:

#include <18f458.h>
#device  adc=10 *=16

#device  ICD=true
#include <STDLIB>

#use   delay(clock=20000000,RESTART_WDT)
#fuses NOWDT, WDT128, HS, PROTECT, NOOSCSEN, BROWNOUT, BORV20, PUT, STVREN, NODEBUG
#fuses NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB

struct
{
int8  thingy1;
int32 thingy2;
} stuff;


typedef struct data_entry_struct
{
   int8  message[16];                       // Message on display
   int8  entry_method;                      // data_entry method
   int8  var_size;                          // var size in bytes(1=int8,2=16,4=32 etc)
   void  *var_ptr;                          // pointer to number location, cast to correct type later using var type
   int8  dp;                                // position of DP on display
   int8  units;                             // units to display after value, ms etc
   int32 min;                               // minimum value
   int32 max;                               // maximum value
};


const struct data_entry_struct const data_entry[2]=
{
//   message         entry method,     var size               var               dp    units    min   max 
   {"Enter thingy1", 1           ,     sizeof(stuff.thingy1), &stuff.thingy1,   2,    0,       0,    100},
   {"Enter thingy2", 2           ,     sizeof(stuff.thingy2), &stuff.thingy2,   0,    0,       0,    10000},
};



void main()
{

int32 temp_data;
int8  data_index;
int8 data_byte_size;

stuff.thingy1=123;
stuff.thingy2=12345;

// loads of code here
data_index=1;  // this is normally set in the code

temp_data=(int32)*data_entry[data_index].var_ptr; // get variable value, this works fine

// code to change temp_data here

data_byte_size=data_entry[data_index].var_size;
// now put value back in variable according to size
if(data_byte_size==1) 
   *(int8)data_entry[data_index].var_ptr=(int8)temp_data; // this works
else if(data_byte_size==4)
   *(int32)data_entry[data_index].var_ptr=temp_data; // this doesn't work, only writes low byte

// more code here
while(1);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 11:31 am     Reply with quote

You need to tell the compiler that 'var_ptr' is a pointer to a 32-bit value.

You have this:
Code:

*(int32)data_entry[data_index].var_ptr = temp_data;


Change it to this:
Code:

*(int32 *)data_entry[data_index].var_ptr = temp_data;



The line for the 8-bit one should also be changed. It should be:
Code:
*(int8 *)data_entry[data_index].var_ptr = (int8)temp_data;
Jim Hearne



Joined: 22 Dec 2003
Posts: 109
Location: West Sussex, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Oct 20, 2006 1:31 pm     Reply with quote

Many thanks PCM for that quick fix, i knew it was something silly, i was close.

Thinking about it on the way home from work i think i need to cast the pointer for the variable read as well as it's probably reading 32 bits at the moment and only seems to be working because most of the variables are 0.

Despite programming with CCS for the last 4 years and Borland C for many years before that i still have trouble getting my head more complex pointer usage.
Has anybody got any pointers Smile to some good reading on the subject.
Most stuff now seems to be more related to c++ and windows programming and that confuses me even more.

Many thanks,

Jim Hearne
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 1:53 pm     Reply with quote

Here's an explanation of the C language that's written in an
engineer to engineer" style. It might be helpful.
http://cslibrary.stanford.edu/101/EssentialC.pdf
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