I am trying to work out the best syntax for a relatively simple operation. The goal is to declare an instance of a struct and pass it around to be populated and have the data manipulated. There is an extra wrinkle in that the functions are in different src files.
The main is simple,
#include <iostream>
#include <stdio.h>
#include "struct1.h"
#include "extern_funcs.h"
int main(int argc, char**argv) {
// declarations
STRUCT1 myStruct1;
int answer;
int a, b, c, d;
// load data
a = 1; b = 2; c = 3; d = 4;
// call functionto load data into stuct
load_struct( &myStruct1, a, b, c, d );
// get answer
answer = calc_struct( &myStruct1 );
std::cout << "the answer is " << answer << std::endl;
return 0;
}
With the struct definition and the prototypes for the external functions in header files.
// struct1.h
// structure definition
typedef struct STRUCT1 {
int var_a;
int var_b;
int var_c;
int var_d;
} STRUCT1;
// external_funcs.h
// prototypes for external functions
extern "C" {
void load_struct( STRUCT1 *myStruct1,
int var_a, int var_b, int var_c, int var_d );
int calc_struct( STRUCT1 *myStruct1 );
}
Then there are two other functions, one to load data into the struct, and the other to do a simple calculation based on the loaded data.
// load_struct function
#include <stdio.h>
#include "struct1.h"
void load_struct( STRUCT1 *myStruct1, int var_a, int var_b, int var_c, int var_d)
// load struct with passed data
myStruct1.var_a = var_a;
myStruct1.var_b = var_b;
myStruct1.var_c = var_c;
myStruct1.var_d = var_d;
return;
}
#include <stdio.h>
#include "struct1.h"
int calc_struct( STRUCT1 *myStruct1 ) {
int answer;
answer = myStruct1.var_a + myStruct1.var_b + myStruct1.var_c + myStruct1.var_d;
return answer;
}
As usual, I am a bit confused about passing by reference, pointer, value, etc. The main function compiles, but load_struct does not and gives the following errors.
load_struct.cpp:8: error: expected init-declarator before "myStruct1"
load_struct.cpp:8: error: expected `,' or `;' before "myStruct1"
load_struct.cpp:9: error: expected constructor, destructor, or type conversion before '.' token
load_struct.cpp:9: error: expected `,' or `;' before '.' token
load_struct.cpp:10: error: expected constructor, destructor, or type conversion before '.' token
load_struct.cpp:10: error: expected `,' or `;' before '.' token
load_struct.cpp:11: error: expected constructor, destructor, or type conversion before '.' token
load_struct.cpp:11: error: expected `,' or `;' before '.' token
load_struct.cpp:13: error: expected unqualified-id before "return"
load_struct.cpp:13: error: expected `,' or `;' before "return"
load_struct.cpp:15: error: expected declaration before '}' token
make: *** [load_struct.o] Error 1
The compiler doesn't appear to like the way I am doing the assignments. I don't know if this is because I have the improper syntax for the assignment, or if I have not passed the struct properly. It also appears that it is necessary to include the header file with the struct definition in all the src files, is that correct?
I have attached the src, including a make, if anyone is interested. Suggestions would be very much appreciated,
LMHmedchem