What happens if I have a method declaration of:

int method( char * a, char * b, int * c );

and then I pass in:

char a;
char b;
int c;
method(a, b, &c);

This has me a little puzzled. What values will be passed by reference?

Recommended Answers

All 7 Replies

The code is incorrect and the compiler will puke out errors at you. All parameters must be passed by reference. method(&a, &b, &c);

Or you can explicitly make pointers and pass those by value.

char* a;
   char* b;
   int* c;
   method(a, b, c);

Ok, I see now. They're using LPSTR, something I've never heard of. I'm thinking this whole component needs updating.

That probably will compile but won't link properly. You are attempting to pass in actual objects instead of pointers to objects which causes the linker to look for a different overloaded version of the function/method. If it does compile & link successfully, you'll probably get memory errors and/or crashes. For the declaration you have, this would be the proper call:

char a;
char b;
int c;
method(&a, &b, &c);

With the proper call, a, b, and c in method() would be storing the memory addresses of a, b, and c in the previous scope.

A different, simpler, declaration for a pass by reference would be:

int method( char &a, char &b, int &c );

Then the call would be:

char a;
char b;
int c;
method(a, b, c);

EDIT:
Oops, some overlap.

>>They're using LPSTR, something I've never heard of. I'm thinking this whole component needs updating
AD would have to confirm, as he does a lot with windows programming, but I believe LPSTR is a Windows API typedef for char*. The Windows APIs use a lot of strange type names like that.

Preferably use Ancient Dragon's response. Or instead of pointers use reference.

int method (char &a, char &b, int &c);
char a,
b;
int c;
method(a,b,c);

LPSTR is just a MS-Windows #define for char*.

Ok, makes more sense now. I guess that's where I was getting confused. Now that I know they're actually passing in char* and not just char, I think my question isn't relevant anymore. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.