Passing a pointer to a Struct to a function

A little background.

I am using a mega Processor with a Rugged Circuits external memory board.

Currently, I am storing nine(9) structs containing four members each in the memory board. Since the structs are stored in the memory board, I have to tell the processor where to put them and where to go and get the data. I currently have all of that working fine.

The Structs are setup as follows:

struct { 
       byte DataArray1[501][62]; 
       float MapLat; 
       float MapLong; 
       String FileNam;

       }*PtrDataArray_2d_1 = 8704, *PtrDataArray_2d_2 = 8704, //Assigns array to memory location
        *PtrDataArray_2d_3 = 8704, *PtrDataArray_2d_4 = 8704, *PtrDataArray_2d_5 = 8704,
        *PtrDataArray_2d_6 = 8704, *PtrDataArray_2d_7 = 8704;

I am currently accessing the Structs as follows:

// Write to a member        
        PtrDataArray_2d_6->DataArray1[y][x] = Value3;

// Read from a member 
         Value3 = PtrDataArray_2d_6->DataArray1[y][x] ;

This of course is being done within functions and works fine as long as everything is local and all of the variables involved are globally visible. None of the variables are currently passed to or from the functions.

The program is getting large and it is time to start moving finished sections of code to .cpp files. In order for me to do that, all of the variables used in the functions will have to be passed to the functions and of course declared in the .h file.

I have spent all day looking in various places trying to get this to work but am not getting anywhere.

I have tried all of the following and so far I cannot get anything to compile.

void Convert(struct *PtrDataArray_2d_6) 

void Convert(struct (*PtrDataArray_2d_6))

void Convert(struct PtrDataArray_2d_6) 
 
void Convert(struct &PtrDataArray_2d_6)

Options 1 and 2 seem to me to be the best guess but no luck. The compiler wants a token before "*".

Option 4 was a shot in the dark and the compiler again complained about a token problem.

I know there has got to be a way to get this done. Any ideas?

Thanks for your assistance in advance

I have tried all of the following and so far I cannot get anything to compile.

You need to tell the function more than that the pointer is a pointer to a struct.

You need to tell it what kind of struct it is a pointer to. Since you have an anonymous struct, that is not going to be possible.

You need to define a named struct, so you can pass a pointer to it to the function(s).

I didn't say it but I tried that but I did try the following:

struct Data{
      byte DataArray1[501][62];
      float MapLat;
      float MapLong;
      String FileNam;

      }*PtrDataArray_2d_1 = 8704, *PtrDataArray_2d_2 = 8704, //Assigns array to memory location
       *PtrDataArray_2d_3 = 8704, *PtrDataArray_2d_4 = 8704, *PtrDataArray_2d_5 = 8704,
       *PtrDataArray_2d_6 = 8704, *PtrDataArray_2d_7 = 8704;
void Convert(struct Data *PtrDataArray_2d_6)

It didn't work but I just got variant of that to compile.

When I tried to setup a call for the function, I got into trouble again. The following is what I tried:

// Option 1
Convert(ScreenLabel, TextHdrTbl, DATA *PtrDataArray_2d_6);

//Option 2
Convert(ScreenLabel, TextHdrTbl,  *PtrDataArray_2d_6);

//Option 3
Convert(ScreenLabel, TextHdrTbl,  Data (*PtrDataArray_2d_6));

//Option 4
Convert(ScreenLabel, TextHdrTbl,  Data);

Basically the compiler came up with the same error. It wants a Primary-Expression before the "*"

Well, I'm half way there (I think)

Thanks for the input

Probably not exactly what you had in mind, but you can see the techniques. I don't have your hardware, but this compiles. I also took out your use of the C++ String class before you get yelled at for that :grinning:

Main .ino File:

#include "OtherFile.h"

Data *dataPtr = (Data *) 8704;

void setup() {
  Serial.begin(115200);
  float lat;
  lat = getMapLat(dataPtr);
  Serial.println(lat);
}

void loop() {
}

otherFile.h:

typedef struct {
  byte DataArray1[501][62];
  float MapLat;
  float MapLong;
  char FileNam[20];
} Data;

float getMapLat(Data *);

otherFile.cpp:

#include <Arduino.h>
#include "OtherFile.h"

float getMapLat(Data *ptr) {
  return ptr-> MapLat;
}

Thanks a bunch. Somewhat different than what I am doing but if this works, I'll go with it.

The Structs are used by several different functions at different times for different reasons. I also expect the various function will be in several .h/.cpp file pairs.

I see that you only have one name for the struct. I am not sure that is an issue. I am storing nine (9) of them
in different memory locations. I have complete control over where they are located so the name is more of a convenience than anything else. As for the String used for the file name. I did that because it is easy for me to load and the filename has to be in the String format for the SD controller to use. The file name actually has data in it that I use and I have to strip the string apart to get the data I need so being in an array of char is not a problem. The length of the array is also fixed as the SD library only uses short file names.

Thanks again for the input!!

KenK:
I see that you only have one name for the struct. I am not sure that is an issue. I am storing nine (9) of them in different memory locations.

You only need one name per data type. You can have as many instances of the type as you want. Just like you can have as many variables of type 'int' as you want. You don't worry about having only one 'int' type do you?

I understand. It took a little while to sink in. As you stated, the struct becomes a data type no different that an "int" a "byte" or a "float". Just assign it a name as you would any other data type.

As for the pointer, one will do as the base address for all of the structs will be the same. I control which one of the 16-32k pages the individual structs get sent to.

I will study this and try to code it this afternoon. It will take a little work for me to change my current program around to work with this but not that much.

Again thanks very much for the help with this.

KenK:
As for the pointer, one will do as the base address for all of the structs will be the same.

You can define as many pointers as you want too. They're just variables of type "Data *".

Thanks a bunch

I haven't had time to code, run and test this yet but will do so today

Thanks again

I had a chance to implement your example code yesterday. It took me a while but I got it running but I got it running.

First coded it locally and when I had that running, I moved the code to a set of ".h/.cpp" files.

The only change I made to the code was to access the data structure directly instead of through a function as you did.

I am reading and writing to the structure directly as follows:

 while (ImageFileIn.available()) {
           for (y =0; y <= 500; y++){
               for (x =0; x<= 61; x++){
                   dataPtr->DataArray1[y][x] = ImageFileIn.read();
 
                   )
              }
        } 
 dataPtr->MapLat = MapLocation0(); 
 dataPtr->MapLong = MapLocation1(); 

 dataPtr->FileNam = Infile;

Doing this allows me to eliminate a couple of float variables and a I need all the RAM I can get for VARs.

Thanks again for your help.