Array of structures

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 10
Reputation: psyman_86 is an unknown quantity at this point 
Solved Threads: 0
psyman_86 psyman_86 is offline Offline
Newbie Poster

Array of structures

 
0
  #1
Sep 30th, 2008
Hi, I have a structure in a library that defines various parameters for wireless transmission. I want to create an array of these structures, which each element in the array containing the required parameters for a particular wireless channel.

The structure:
  1. typedef struct S_RF_RXTXPAIR_SETTINGS {
  2. byte modem0; // Baudrate, xosc_freq, dataformat
  3. byte modem1; // settling, peakdetect
  4. byte modem2; // Peak level offset
  5. byte freq_2a; // RX channel frequency (A)
  6. byte freq_1a;
  7. byte freq_0a;
  8. byte freq_2b; // TX channel frequency (B)
  9. byte freq_1b;
  10. byte freq_0b;
  11. byte fsep1; // Frequency seperation
  12. byte fsep0;
  13. byte pll_rx; // refdiv
  14. byte pll_tx; // refdiv
  15. byte current_rx; // vco_current, lo_drive, pa_drive
  16. byte current_tx; // vco_current, lo_drive, pa_drive
  17. byte frend; // buf_current, lna_current
  18. byte pa_pow; // TX output power
  19. byte match; // rx_match, tx_match
  20. byte prescaler; // pre_swing, pre_current
  21. } RF_RXTXPAIR_SETTINGS;

My attempt at defining the array is as follows:
  1. RF_RXTXPAIR_SETTINGS code RF_SETTINGS[2];
  2. RF_SETTINGS[1] = {0xA8,0x2F,0x37,0x50,0xA0,0x00,0x41,0xF6,0x10,0x02,0x80,0x58,0x48,0x44,0x81,0x0A,0xFF,0xC0,0x00};
  3. RF_SETTINGS[2] = {0xA8,0x2F,0x37,0x50,0xA8,0x00,0x41,0xF6,0x10,0x02,0x80,0x58,0x48,0x44,0x81,0x0A,0xFF,0xC0,0x00};

It has been a while since i have done any C programming, an i cant seem to find a good example of what i am trying to do. Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Array of structures

 
0
  #2
Sep 30th, 2008
What you are doing will work i.e. RF_RXTXPAIR_SETTINGS code RF_SETTINGS[2];

If you want to dynamically assign an array size at runtime, you could do

RF_SETTINGS = malloc (number_of_elements * sizeof(RF_RXTXPAIR_SETTINGS));
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Array of structures

 
0
  #3
Sep 30th, 2008
Well the first thing you need to remember is arrays start at 0, not 1.

2. Arrays can be initialised, not assigned, eg
  1. RF_RXTXPAIR_SETTINGS RF_SETTINGS[2] = {
  2. {0xA8,0x2F,0x37,0x50,0xA0,0x00,0x41,0xF6,
  3. 0x10,0x02,0x80,0x58,0x48,0x44,0x81,0x0A,
  4. 0xFF,0xC0,0x00},
  5. {0xA8,0x2F,0x37,0x50,0xA8,0x00,0x41,0xF6,
  6. 0x10,0x02,0x80,0x58,0x48,0x44,0x81,0x0A,
  7. 0xFF,0xC0,0x00}
  8. };
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1465 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC