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

problem with pointers

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



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

problem with pointers
PostPosted: Thu Nov 11, 2004 11:03 am     Reply with quote

I have a problem with this code. Can someone see the error?
ccs ver 3.212 and a 16f877 @ 16MhZ
When I step through it, the watch window is correct. But on the rs232 debug window, I don't get 70,80,90 as I expect.

Code:
#include <16F877.h>
#device *=16
#include <string.h> //for memcmp
#case
#use delay(clock=16000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#ZERO_RAM
#define VER_MAJOR 1
#define VER_MINOR 00
#use rs232(baud=57600,xmit=PIN_A2,INVERT,stream=DEBUG)

struct  pkt
{   
  BYTE type;       
  BYTE data[25];       
};
struct pkt Pkt0,Pkt1,Pkt2,Pkt3;
struct pkt *p_pkt[4];


#define TXSIZE   32
#define TXMASK TXSIZE-1
int8 TX_BUF[TXSIZE];
int8 tx_indx_i,tx_indx_o;

#define RXSIZE   16
#define RXMASK RXSIZE-1
int8 RX_BUF[RXSIZE];
int8 rx_indx_i=0,rx_indx_o=0;

//======================= MAIN ============================//
void main(void)
{
  setup_adc_ports(NO_ANALOGS);
  set_tris_a(0xFF);
  set_tris_b(0xFF);
  set_tris_c(0xFF);
  set_tris_d(0xFF);
  set_tris_e(0xFF);
  p_pkt[0]=&Pkt0;
  p_pkt[1]=&Pkt1;
  p_pkt[2]=&Pkt2;
  p_pkt[3]=&Pkt3;
  //--------- START -------//
  fprintf(DEBUG,"STARTING U11\n\r");
  fprintf(DEBUG,"FIRMWARE VERSION %u.%02u\n\r",VER_MAJOR,VER_MINOR);

  Pkt0.type =  0x10;
  Pkt0.data[0]=0x20;
  Pkt0.data[1]=0x30;
  Pkt1.type =  0x70;
  Pkt1.data[0]=0x80;
  Pkt1.data[1]=0x90;
 
  fprintf(DEBUG,"%X %X %X\n\r",Pkt0.type,Pkt0.data[0],Pkt0.data[1]);
  fprintf(DEBUG,"%X %X %X\n\r",Pkt1.type,Pkt1.data[0],Pkt1.data[1]);
  fprintf(DEBUG,"----------\n\r");
  fprintf(DEBUG,"%X \n\r",p_pkt[0]->type);
  fprintf(DEBUG,"%X \n\r",p_pkt[0]->data[0]);
  fprintf(DEBUG,"%X \n\r",p_pkt[0]->data[1]);
  fprintf(DEBUG,"----------\n\r");
  fprintf(DEBUG,"%X \n\r",p_pkt[1]->type);
  fprintf(DEBUG,"%X \n\r",p_pkt[1]->data[0]);
  fprintf(DEBUG,"%X \n\r",p_pkt[1]->data[1]);
 
while(1){}
}

Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 11, 2004 1:11 pm     Reply with quote

Code:
#include <16F877.h>
#device *=16
#include <string.h> //for memcmp
#case
#use delay(clock=16000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#ZERO_RAM
#define VER_MAJOR 1
#define VER_MINOR 00
#use rs232(baud=57600,xmit=PIN_A2, INVERT, stream=DEBUG)

struct  pkt
{   
  BYTE type;       
  BYTE data[25];       
};
struct pkt Pkt0,Pkt1,Pkt2,Pkt3;
struct pkt* p_pkt[4];


#define TXSIZE   32
#define TXMASK TXSIZE-1
int8 TX_BUF[TXSIZE];
int8 tx_indx_i,tx_indx_o;

#define RXSIZE   16
#define RXMASK RXSIZE-1
int8 RX_BUF[RXSIZE];
int8 rx_indx_i=0,rx_indx_o=0;

//======================= MAIN ============================//
void main(void)
{
  setup_adc_ports(NO_ANALOGS);
  set_tris_a(0xFF);
  set_tris_b(0xFF);
  set_tris_c(0xFF);
  set_tris_d(0xFF);
  set_tris_e(0xFF);
  p_pkt[0]=&Pkt0;
  p_pkt[1]=&Pkt1;
  p_pkt[2]=&Pkt2;
  p_pkt[3]=&Pkt3;
  //--------- START -------//
  fprintf(DEBUG,"STARTING U11\n\r");
  fprintf(DEBUG,"FIRMWARE VERSION %u.%02u\n\r",VER_MAJOR,VER_MINOR);

  Pkt0.type =  0x10;
  Pkt0.data[0]=0x20;
  Pkt0.data[1]=0x30;
  Pkt1.type =  0x70;
  Pkt1.data[0]=0x80;
  Pkt1.data[1]=0x90;

  fprintf(DEBUG,"%X %X %X\n\r",Pkt0.type,Pkt0.data[0],Pkt0.data[1]);
  fprintf(DEBUG,"%X %X %X\n\r",Pkt1.type,Pkt1.data[0],Pkt1.data[1]);
  fprintf(DEBUG,"----------\n\r");
  fprintf(DEBUG,"%X \n\r",(p_pkt[0])->type);
  fprintf(DEBUG,"%X \n\r",(p_pkt[0])->data[0]);
  fprintf(DEBUG,"%X \n\r",(p_pkt[0])->data[1]);
  fprintf(DEBUG,"----------\n\r");
  fprintf(DEBUG,"%X \n\r",(p_pkt[1])->type);
  fprintf(DEBUG,"%X \n\r",(p_pkt[1])->data[0]);
  fprintf(DEBUG,"%X \n\r",(p_pkt[1])->data[1]);
 
while(1){}
}
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 11, 2004 1:30 pm     Reply with quote

THANKS, THANKS,THANKS, THANKS,THANKS, THANKS!

order of evaluation, I get it...

never saw that comming.
: )


Last edited by treitmey on Wed Jan 11, 2006 10:14 am; edited 1 time in total
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 11, 2004 2:37 pm     Reply with quote

Welcome, welcome, welcome.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 11, 2004 3:59 pm     Reply with quote

Perhaps someone can help me on this also. Why won't this work. : (
Code:
#include <16F877.h>
#device *=16
#include <string.h> //for memcmp
#case
#use delay(clock=16000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#ZERO_RAM
#define VER_MAJOR 1
#define VER_MINOR 00
#use rs232(baud=57600,xmit=PIN_A2,INVERT,stream=DEBUG)

struct  pkt
{   
  BYTE type;       
  BYTE data[25];       
};
struct pkt Pkt0,Pkt1,Pkt2,Pkt3;
struct pkt * p_pkt[4];
//4 structs and an array of 4 pointers to the stucts
//======================= MAIN ============================//
void main(void)
{
  int8 i,n;
  setup_adc_ports(NO_ANALOGS);
  set_tris_a(0xFF);
  set_tris_b(0xFF);
  set_tris_c(0xFF);
  set_tris_d(0xFF);
  set_tris_e(0xFF);
  p_pkt[0]=&Pkt0;
  p_pkt[1]=&Pkt1;
  p_pkt[2]=&Pkt2;
  p_pkt[3]=&Pkt3;
  //--------- START -------//
  fprintf(DEBUG,"STARTING U11\n\r");
  fprintf(DEBUG,"FIRMWARE VERSION %u.%02u\n\r",VER_MAJOR,VER_MINOR);

  Pkt0.data[1]=0x1;
  Pkt1.data[1]=0x2;
  Pkt2.data[1]=0x3;
  Pkt3.data[1]=0x4;
 
  fprintf(DEBUG,"%X %X %X %X\n\r",Pkt0.data[1],Pkt1.data[1],Pkt2.data[1],Pkt3.data[1]);

  fprintf(DEBUG,"----------\n\r");
  for (i=0;i<4;i++)
  {
    fprintf(DEBUG,"%X \n\r",(p_pkt[i])->data[1]);
  }
 
while(1){}
}
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 11, 2004 5:27 pm     Reply with quote

Can I simply not have 4 structs and a 4 elliment array of pointers to the struct?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 11, 2004 8:22 pm     Reply with quote

Just a stinky compiler Confused

Code:

  for (i=0;i<4;i++)
  {
    fprintf(DEBUG,"%X \n\r", ((struct pkt*)p_pkt[i])->data[1]);
  }
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Nov 12, 2004 8:16 am     Reply with quote

So if i understand.
You typecast p_pkt to be of type pointer to struc pkt.
and then you wraped the part in front of the structure pointer operator '->'

Got it.
Thanks
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Nov 12, 2004 8:38 am     Reply with quote

You got it. Very Happy
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