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

putting structs into the arrays

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



Joined: 24 Sep 2013
Posts: 16

View user's profile Send private message

putting structs into the arrays
PostPosted: Fri Apr 17, 2015 1:29 am     Reply with quote

Hi to all,
How can i put/get structs into/from arrays?

For example struct and array definitions are like that:
Code:

struct rx_stat {
   int1 err_ovfl;          // buffer overflow
   unsigned int filthit;   // filter that allowed the frame into the buffer
   unsigned int  buffer;   // receive buffer
   int1 rtr;               // rtr requested
   int1 ext;               // extended id
   int1 inv;               // invalid id?
};

int8 can_rstack [CAN_RECEIVE_STACK_SIZE] [14];


Thanks in advance.
ZD
ZD



Joined: 24 Sep 2013
Posts: 16

View user's profile Send private message

PostPosted: Fri Apr 17, 2015 2:21 am     Reply with quote

Hi everybody again,
I found the answer. It can be done by memcpy command.
Code:

   memcpy(&can_rstack[0][0], &rx_stat, sizeof(rx_stat));
   memcpy(&tx_stat, &can_rstack[0][0], sizeof(rx_stat));

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 1:11 pm     Reply with quote

However this is not how to do it normally....

Use a union.

This allows you to allocate both the structure and the integer array, to the _same_ area of memory. Data written into the array, is then also in the structure, without wasting time moving the data around, and the space for two copies of the data.
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Mon Apr 20, 2015 4:29 am     Reply with quote

@Ttelmah

could you post some code to illustrate the use of a union in this case.

I never understood the use of unions
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Mon Apr 20, 2015 7:51 am     Reply with quote

For the union, just read a C manual.

However if you have the code already declared, and don't want to go changing the variable names, you can also use the #byte directive.

#byte creates a byte sized variable at a location. However it can also locate a variable at a location. So just declare rx_stat, and then use a byte directive to locate this at the location of the can buffer.

Just add this line after your variable declarations:

#byte rx_stat=can_rstack

And the two variables will be put into the same memory area.
ZD



Joined: 24 Sep 2013
Posts: 16

View user's profile Send private message

PostPosted: Tue Jun 09, 2015 1:31 am     Reply with quote

The data position at the can_rstack is always changing. So, i don't understand how can we use unions.
Is it possible to give an example.

Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Tue Jun 09, 2015 2:10 am     Reply with quote

Look at your memcpy.

In the first line, you copy from the address of 'rx_stat' to the location of the can_rstack array. No 'moving'. The variables are in the same locations....
ZD



Joined: 24 Sep 2013
Posts: 16

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 2:09 am     Reply with quote

I understand what you mean. But i'm confused about arrays. Because, it is two dimension array and there are too many pieces same struct. So, how the two register share the same register position.

Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Wed Jun 10, 2015 2:41 am     Reply with quote

With the union, space is allocated for the largest block of data. Then the other blocks are all aligned to the same space. If they are not the same size, then they will start at the bottom, and end prematurely - you can always add dummy 'padding' variables.

For the #byte, the second variables start location is placed wherever you tell it. Can be start, middle, end of the other variable.

Variable names are not 'registers'. They are just titles used to give you a bookmark into the memory.

Imagine you are organising a running event. Round a local park. You call it the 'blotto run' (a title), since you know a lot of drink will be consumed. You put markers round the park, with the distances to the finish etc.. A year on, and it grows, with other groups wanting to also be involved. You find you have a group of children, wanting to do a shorter race. So the decision is made this year, to have them run the second half of the course. A second set of markers is added in another colour, and the 'milk run' is added. The next year, paraplegic competitors also want to be involved. Unfortunately there is a nasty skiddy bit at the end that would be difficult if not impossible in a wheel chair, so a second ending is added just short of this, with another set of different markers, and the 'wheee bit' is born. Now you could have done all the events on completely different courses, but then the spectators couldn't follow them all (this is the 'memcpy' approach), with multiple locations each holding their own event. The union/#byte approach is just putting the different markers onto the same course (and having the runners start at different times etc..). Re-use of resources.
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