I extended the exercise to have the calc function called by the loading function and was surprised to find that you don't use the & (if you want it to compile).
The & operator turns a vartype into a vartype *. Your function already has a vartype *, using & on that turns it into a vartype **varname -- which actually ends up pointing to the local pointer variable inside your function. C++ notices you're trying to put a pointer of the wrong type into it and refuses to let you, which is good, since that'd probably crash. In C, this is only a warning.
Quote:
Is that because the compiler already sees the value as pointer, and so would be trying to do **myStruct1?
When you do a function declaration, int calc_struct( STRUCT1 *myStruct1 ) that "STRUCT *myStruct1" declares a local variable named myStruct1, of the the type "pointer". That's just a numeric variable with enough bits to hold a memory address on your architecture. You already have a pointer and really couldn't have anything else but a pointer if you gave it one -- pointer operations are always done explicitly, the compiler will never decide to make a pointer a not-pointer for you (or vice-versa).
In other words, pointers never do anything weird behind your back, don't use their contents for you, don't change into different types for you. They're just numbers, they just sit there. All that's different is their *, &, and [] operators.
Quote:
Would this work just as well if the prototypes were absent the extern "C" tag?
I think so.
What extern "C" does is it forces the function to have that exact same in the compiled program. Without it, C++ will give it some crazy hashed name, which lets you do operator overloading -- different functions with the same text name won't have the literal same name in the program, and so won't interfere with each other, as long as they take different parameters.
Quote:
In the past, I have had a few issues with things in header files where the compiler complained that something was declared in more than one place.
This is unrelated to extern "C". What it means is you put the actual function inside the header file, so it got compiled separately in every source file that used it. Or declared a variable in the header without declaring it as extern.
Quote:
I have also has some issues with the linker finding things when the src is spread across multiple src files. I make enough little errors that I can't always be sure if the issue is my choice of method, or its implementation. It appears that it is proper to have struct/class definitions, or function prototypes, in header files that are included in multiple places. Is that correct?
Yes, exactly. Don't define the contents of functions or variables or objects or members in headers -- all headers should do is state that they exist.
The same goes for variables if you want to share them between different files.
Last edited by Corona688; 04-01-2011 at 04:39 PM..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)