473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to pass an array of struct's to a function?

2
I'm currently strugling with a a problem,
I have an array of structs like so:

Expand|Select|Wrap|Line Numbers
  1. //populates Message control object(st_cmd_t)and acts 
  2. //as a container for the sourrounding data
  3. CanMsgObj* Populate_CanMsgObj()
  4. {
  5.     //make a pointer to required structure
  6.     CanMsgObj *MsgObj;
  7.     //assign memory to structure
  8.     MsgObj =  malloc(sizeof(CanMsgObj));
  9.     //assign memory to the st_cmd_t
  10.     MsgObj->interface=Prepare_st_cmd_t();
  11.     //Set Default Action
  12.     MsgObj->mode=MODE_SEND;
  13.     //zero the timer
  14.     MsgObj->timer=1;
  15.     MsgObj->local_time_overrun=0;
  16.     //is the timer translated to local time? 0-no, 1-yes
  17.     MsgObj->local_time=0;
  18.     //set the mask to accept only this address
  19.     long unsigned u32_temp;
  20.     u32_temp=~0;
  21.     MsgObj->mask=u32_temp;
  22.     //zwroc obiekt
  23.     return MsgObj;
  24. }
  25.  
  26. //prepares the st_cmd_t structure by allocating memory
  27. st_cmd_t* Prepare_st_cmd_t()
  28. {
  29.     //create a message object pointer
  30.     st_cmd_t *msg;
  31.     //allocate memory
  32.     msg =  malloc(sizeof(st_cmd_t));
  33.     //create a pointer to data table
  34.     U8 *buffer;
  35.     //create space for data
  36.     buffer = malloc(sizeof(U8)*8);
  37.     //set the allcocated space to 0's
  38.     for(int i=0; i<8; i++)
  39.     {
  40.         buffer[i]=7-(unsigned char)i;
  41.     }
  42.     //assign table to message
  43.     msg->pt_data = &buffer[0];
  44.     //mark data lenght 0
  45.     msg->dlc = 8;
  46.     //set default command mode (receive)
  47.     msg->cmd = CMD_TX;
  48.     //default CAN message number (x1234)
  49.     int id=123;
  50.     msg->id.std = id;
  51.     //default to 11bit numbering
  52.     msg->ctrl.ide = 0;
  53.     //not a request frame
  54.     msg->ctrl.rtr = 0;
  55.     //return created and allocated st_cmd_t structure
  56.     return msg;
  57. }
  58.  
  59. //create a CanMsgObjArray
  60. volatile CanMsgObj *CanObjArray[15];
  61.  
  62. //populate CanMsgObjArray
  63. for (int i=0; i<16; i++)
  64. {
  65.     CanObjArray[i]=Populate_CanMsgObj();
  66. }
  67.  
  68. //create a UART_MessageTracker pointer
  69. UART_MessageTracker *UART_Tracker;
  70.  
  71. //prepare the tracker
  72. UART_MessageTracker* CreateUART_MessageTracker()
  73. {
  74.     UART_MessageTracker *Tracker;
  75.     Tracker =  malloc(sizeof(UART_MessageTracker));
  76.     Tracker->PropGroup=0;
  77.       Tracker->MObj_no=0;
  78.     Tracker->cnt=0;
  79.     return Tracker;
  80. }
  81.  
  82. //USART1 byte recevied interrupt handling
  83. ISR(USART1_RX_vect)
  84. {
  85.     UART_hit(UART_Tracker, CanObjArray[UART_Tracker->MObj_no]);
  86. }
  87.  
  88.  
  89. main()
  90. {
  91. while(1);
  92. }
  93.  
  94.  
Main just runs in a loop and "ISR(USART1_RX_vect)" is triggered when a message is received over UART

The question is : how to pass the CanObjArray to UART_hit so I can have access to all of the 15 structs inside?

I'm really stuck here :(
Thanks in advance for your suggestions!

(gucio)
May 4 '10 #1
2 2864
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. volatile CanMsgObj *CanObjArray[15]; 
is an array of 15 CanMagObj pointers and not an array of 15 structs.

Did you mean:

Expand|Select|Wrap|Line Numbers
  1. volatile CanMsgObj CanObjArray[15]; 
?

If so, you pass the array as a CanMsgObj* function argument.
May 5 '10 #2
gucio
2
Thanks!
I finally worked it out last night around 4:30 in the morning.
I never suspected that Gun Powder tea is so damn strong!

Thank You very much for your help!

gucio
May 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
2
by: Matthew Louden | last post by:
When I pass an array as a function parameter, it yields the following compile error. Any ideas?? However, if I create a variable that holds an array, and pass that variable to the function...
10
by: tony | last post by:
i'm trying to itterate through an array that contains the names of the global arrays eg: $myarray = array("\$_GET", "\$_SERVER"); and so on The problem Im having is calling a function...
6
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
3
by: bhejafry.mmk | last post by:
hey .. i've been tryin this for a long time now .. jow the bloody hell do i pass an array to the function .. also .. how do i get the function to return an array ... all in C language ... please...
5
by: FrederikVds | last post by:
I have a function which can be called with an unlimited number of arguments. I want to call another function with exactly the same arguments. I know I can get the arguments in the arguments...
1
by: sharp learner | last post by:
Hello I am writing source code in visual studio.net( c++) in a single project I made files named as main.cpp functions_def.h and a global variable let's say: int gArr_var; and have included...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
8
by: Hypnotik | last post by:
Hey everyone. I need to pass a struct array to a function, and calculate the average of one of the members of the struct. My code is posted below. With the current code, it takes the last value...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.