0

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?

Edited by Duki: n/a

5
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Duki
0

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

0

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

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

Edited by Moschops: n/a

0

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

0

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.

Edited by Fbody: n/a

0

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);
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.