jasonline
jasonline

Reputation: 9054

Passing by variable through an external library

How do you perform pass by variable on an interface function provided by a C library?

For example, I have the following interface function:
void f( double *d ) { *d = 7.5; }

In my application, I link through that library and call the function:
double p = 0;
f( &p );

However, I noticed p is still 0 and not 7.5.

Upvotes: 0

Views: 232

Answers (2)

Rafe Kettler
Rafe Kettler

Reputation: 76965

Your example worked, see http://codepad.org/JJL7eab5.

The only change I made was that I declared f() before defining it:

void f(double *);

It would also work if the function definition were declared as static. In C, functions need to be either declared before their definition or declared as static.

Upvotes: 0

pmg
pmg

Reputation: 108978

Do you have the prototype for f in scope, in your application?

If you haven't the compiler converts the address of p to an int; and if they aren't the same size or use the same passing conventions or whatever, you have problems.

You should #include "the_proper_header.h" or you may directly add the prototype in your application, right before you call the function.

double p = 0;
void f(double *);
f( &p );

Upvotes: 2

Related Questions