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

Accessing struct byte by byte

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



Joined: 17 Mar 2004
Posts: 2

View user's profile Send private message

Accessing struct byte by byte
PostPosted: Wed Mar 17, 2004 11:49 am     Reply with quote

Hello,

I have seen several threads on the forum addressing this same problem, but none of their solutions seem to work for me. I have a simple struct that I would like to access byte by byte (for the purpose of transmitting it). It seems to me that I should just be able to access the fifth byte by

my_struct[4]

but that doesn't work (it always returns 0). It also seems like:

int8 *ptr;
ptr = (int8 *)my_struct;
ptr+=4;
fifth_value = *ptr;

should work, but that doesn't either (also giving 0 as the value).

The specific struct I have been using for testing is:

struct t_t {
int t1;
int t2;
int t3;
int t4;
int t5;
int t6;
};

and I have initialized all of the variables in the struct to 1.

Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 17, 2004 12:00 pm     Reply with quote

Use a union of your struct and a byte array. See below.

The following program displays this on the terminal screen.
1 2 3 4 5 6
1 2 3 4 5 6

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

union
{
char tt_array[6];

struct t_t {
int t1;
int t2;
int t3;
int t4;
int t5;
int t6;
}tt_struct;

}tt_union;


//=============================================
void main(void)
{
char i;

// Initialize the values.
tt_union.tt_struct.t1 = 1;
tt_union.tt_struct.t2 = 2;
tt_union.tt_struct.t3 = 3;
tt_union.tt_struct.t4 = 4;
tt_union.tt_struct.t5 = 5;
tt_union.tt_struct.t6 = 6;

// Print structure elements
printf("%d ", tt_union.tt_struct.t1);
printf("%d ", tt_union.tt_struct.t2);
printf("%d ", tt_union.tt_struct.t3);
printf("%d ", tt_union.tt_struct.t4);
printf("%d ", tt_union.tt_struct.t5);
printf("%d ", tt_union.tt_struct.t6);
printf("\n\r");

// Print array elements
for(i = 0; i < 6; i++)
    printf("%d ", tt_union.tt_array[i]);

while(1);
}
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