Sponsored Content
Top Forums Programming Passing an instance of struct to functions in other src files Post 302509835 by LMHmedchem on Thursday 31st of March 2011 09:40:40 PM
Old 03-31-2011
Passing an instance of struct to functions in other src files

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,
Code:
#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.
Code:
// struct1.h
// structure definition

typedef struct STRUCT1 {
   int var_a;
   int var_b;
   int var_c;
   int var_d;
} STRUCT1;

Code:
// 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.
Code:
// 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;

}

Code:
#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.
Code:
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

passing command line parameters to functions - sh

All, I have a sh script of the following tune: function a () { #functionality.. } function b () { #functionnlity.. } function check () { # this function checks for env and if all fine call build } function usage () { #sh usage details } function build () { #calls either a or b or... (5 Replies)
Discussion started by: vino
5 Replies

2. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

3. UNIX for Advanced & Expert Users

Passing socket struct between kernel threads - module programming

I write kernel module with kernel threads using linux/kthread.h on 2.6.* kernel I tried to passing data between two kernel threads with data argument of kthread_run( fun, data , NAME ); but this is not work I dont know why. I tried many possibility and nothing works. So I thought that... (0 Replies)
Discussion started by: marcintom
0 Replies

4. Homework & Coursework Questions

C++ struct pointers & functions

Hi All, My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
Discussion started by: pondlife
0 Replies

5. UNIX for Dummies Questions & Answers

Passing struct through unix pipe -solved

EDIT: Nevermind, called a friend who is good at this stuff and he figured it out :D Hi all, So I'm trying to teach myself to write programs for unix in c. I am currently creating a program, and I need to pass a struct through a pipe, but I can't figure out how. The struct I want to pass... (0 Replies)
Discussion started by: twnsfn34
0 Replies

6. Homework & Coursework Questions

Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Discussion started by: ignatius3
2 Replies

7. Shell Programming and Scripting

Passing variable to child instance of make

hi, I am trying to export a variable from the parent makefile to a child instance of my makefile within the same directory. the relevant code in my parent makefile is: libvar := $(notdir $(basename ../src/file1)) export libvar .PHONY: library library: make -f makechild libvar=file1... (0 Replies)
Discussion started by: bacpp
0 Replies

8. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

9. Shell Programming and Scripting

Python passing multiple parameters to functions

Hi, I am a beginner in python programming. In my python script have a main function which calls several other functions. The main function gets its input by reading lines from a input text file. I call the main function for every line in input text file through a loop. def main(line): var1... (6 Replies)
Discussion started by: ctrld
6 Replies

10. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies
All times are GMT -4. The time now is 10:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy