I know this is a bit long, but it's a tricky problem. Thanks for looking! I've been working on this far too long. I've done a lot of research and tried a lot of things, hopefully somebody can help explain.
Outline:
Writing in C. I need to get an array of data from an instrument. The size of the array will vary depending on how much data is collected. I can query the number of data points. I've written a function to collect the data (shown below). This works when the function is called directly in main(). Which I use to test. However, this function will be a dll and called from a function within a higher dll. This does not work. I believe because of the variable size.
I'm lost in the details of variable size and pointers. Any help is appreciated.
Functioning code, called from main()
*Note - Below ViInt16 is a signed short type specific to VISA (communication protocol for instrument). Function returns an int as the status/error (set in other code).
header:
int GetSamples (ViInt16 *DataArray, int DataSize);
function:
int GetSamples (ViInt16 *DataArray, int DataSize)
{
ViUInt16 N=0;
ViInt16 Datatemp[DataSize];
viMoveIn (Handle, N, 0, DataSize, Datatemp);
memcpy (DataArray, Datatemp, sizeof(Datatemp);
}
main:
int totalsamples = (PreTrigPts() + AcqPts())
ViInt16 Data [totalsamples];
GetSamples (Data, totalsamples);
This works! I think, only because I can get the total size and then allocate the ViInt16 array from main(). Truthfully, not sure what I'm doing with the pointers, etc. in passing around arrays
The problem is, I'm not sure what to do when it's a function calling a function because, I think, the array needs to be allocated at the top, before I've queried the size. Here is how I am trying to do it (that's not working)
Problem Code
Low level "driver" (called as a dll):
Header and function definition from above.
Functions_header: (what calls the low level driver as dll)
void AutoSample(void); //No returns. Writes to UI in Functions_list.c
Function_list.c:
void AutoSample ()
{
int PreTriggerPts = 0; //Declare all variables at top, or won't compile
int Points = 0;
int TotalSamples = 0
ViInt16 MyData [5] = {0}; //arbitrary since don't know size yet
TotalSamples = (PreTrigPts() + AcqPts());
ViInt16 MyData[TotalSamples]; //Trying to size by reinitializing?
GetSamples (MyData, TotalSamples);
//...calls another function to write MyData to UI...etc. etc.
Main.c:
AutoSample();
Errors
I've gotten a lot of errors as I've tried different things. If needed I can be more specific, but if I try to initialize the variable after I know it's size (like I did in the working code), it tells me that's illegal. Putting the declaration up top fixes that. Now I don't know the size to initialize. From here I've just been hacking away, no more specific. There's something I need to do with pointers and memory but I can't figure out what. Please, any help is appreciated!