Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Functions and Arrays in C language

Status
Not open for further replies.

Old Nick

Advanced Member level 1
Joined
Sep 14, 2007
Messages
479
Helped
68
Reputation
136
Reaction score
18
Trophy points
1,298
Activity points
4,243
I'm currently trying to neaten up some code I've written and make elements of it more portable. The code I've written controls an ADC card (measurment computing) which strips data from camera's we've designed.

We use the functions supplied by the card manufacturer to read the data into an array and when the frames have been grabbed then write the data to a file.
The way the code is now means there is a lot of repetition,

Code:
UDStat = cbAIn (BoardNum, Chan0, Gain, &DataValueA); //sample channel 0
				UDStat = cbToEngUnits (BoardNum, Gain, DataValueA, &EngUnitsA); //convert raw data to voltage
				if (photoNumber==0)//nothing to average - load the array
				{
					DataArrayA[row][column] = EngUnitsA;
				}
				else  //start averaging - (old value + new value)
				{
					DataArrayA[row][column] = (DataArrayA[row][column] + EngUnitsA); //
				}

this has to be repeated for every channel. Now what I want to do is create a function with this code and just pass the channel number etc to the function, which is no problem.
However I'm a bit unsure as to hae to treat the array. I'm currently usin 4 ADC channels, so I want to call the funtion 4 times which requires four arrays. Can I pass an array to a function, fill the array in the function and then have access to the data outside the function?

My memory of C is a bit rusty, but i suspect this is going to require the passing of pointers.......gulp!

I hope I've got my question across clearly(ish) enough.

Any help appreciated,

Cheers,

Nick
 

Re: Functions and Arrays

Hi,
As far as I know, if arrays are defined outside all functions and the Main routine, they would be treated as Global variables and would be available to be used / updated by any function.
Regards,
Laktronics
 

Re: Functions and Arrays

Hi Old Nick,
As laktronics has already pointed out, you can use global arrays which will be accessible to all the functions. In this case you need to differentiate which array to be updated (according to the channel no.) in the function which does the averaging.
Another way would be to pass a pointer to the array you need to be updated as an argument to the function which does the averaging. In this case, you don't need to pass the channel no. The function simply does the necessary averaging and updates in the locations pointed to by the argument passed.

Regards,
Vhn
 

Re: Functions and Arrays

Define the arrays you need and then pass them by reference to your functions. It works.
 

Re: Functions and Arrays

Cheers guys,

I solved this a couple of weeks ago.

Thanks anyway,

Nick
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top