change variable value by reference c++/windows forms - winforms

I'm trying to change a variable value by reference (with a function).
My Function:
void chgVariable(String^ *trgvari,String^ trgvarival)
{
*trgvari = trgvarival;
}
My Variable:
String^ myStringVar;
How do I use:
chgVariable(&myStringVar, gcnew String("Test"));
but i get this error:
Cannot convert parameter 1 from 'cli::interior_ptr' to 'String ^*'

You have the definition of the first argument wrong. It should be:
void chgVariable(String* ^trgvari,String^ trgvarival)
If you start with String^ myStringVar, then &myStringVar becomes a pointer to a String^, which is shown as String*^.
Also, now when you dereference it, you should get String^, like you want.
EDIT:
Unfortunately, C++CLI doesn't allow pointers to handles, so you should make it a reference to a handle like so:
void chgVariable(String^% trgvari,String^ trgvarival)
and change &myStringVar to just myStringVar.
Lots of credit to this other stackoverflow question: Pass an argument by reference in C++/CLI so re-assignment affects the caller

Related

I need to pass a file.txt to a string buffer [duplicate]

I am currently reading on the ways of passing arguments to C functions. And while reading I came to know that in C there are two methods to pass arguments as pass by value and pass by reference.
Then again I read that we can pass a pointer to the variable as a parameter too.
The beginnersbook website this method is mentioned as pass by reference.
In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.
I know that we can pass a pointer variable or the address of the variable with & operator when we need to change or access the original variable. I want to know whether this pass by pointer can also be called pass by reference.
Is pass by pointer also a method of pass by reference?
Technically, all arguments to functions in C are pass by value. The language doesn't have support for true references unlike C++.
Passing by reference can be emulated in C by passing (by value) the address of the variable you want to modify, then subsequently dereferencing that address to modify the pointed-to value. This is not true pass by reference, however.
Pointers in C are references. C 2018 6.2.5 20 says:
… A pointer type describes an object whose value provides a reference to an entity of the referenced type…
When you pass a pointer to a function, you are passing the function a reference to whatever object the pointer points to. The pointer itself is passed by value, but, because the pointer is a reference and you are passing it, the description that you are passing an object by reference is accurate.
Prior to the development of C++, there was no dispute about this; people said they were passing an object by reference to describe passing a pointer to the object. C++ adopted the word “reference” as a name for a new feature in its language, so, in C++ terminology, “reference” generally refers to that feature unless otherwise stated or made apparent by context. However, in C, we are not obliged by C++ terminology, and the original meaning of pass by reference remains accurate.
In general, all arguments to functions in 'c' are passed 'by values'. It means that If an argument is modified in a function, this modification will not be visible by the caller:
void foo(int val) {
...
val = newval;
...
}
void bar() {
foo(4);
}
The value val in the above example can be modified and used in 'foo' but 'bar' will not see the update.
In order to make an update visible in the caller, pointers are used.
void foo(int *arg) {
...
*arg = 5;
...
}
void bar () {
int x = 4;
foo(&x);
// x will be 5 here
}
In the above case the address of 'x' will be passed to the function foo and it will be used inside the function *arg to modify the value of 'x'.
One can modify the value of the pointer inside the foo, since it itself is passed by value
void foo(int *arg) {
arg = newAddress;
}
but function 'bar' will not see this modification as well.
So, there are no real 'references' in 'c', however some people use it in reference to the passing of pointers.

Can Pass by Reference also be called as Pass by Pointer?

I am currently reading on the ways of passing arguments to C functions. And while reading I came to know that in C there are two methods to pass arguments as pass by value and pass by reference.
Then again I read that we can pass a pointer to the variable as a parameter too.
The beginnersbook website this method is mentioned as pass by reference.
In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.
I know that we can pass a pointer variable or the address of the variable with & operator when we need to change or access the original variable. I want to know whether this pass by pointer can also be called pass by reference.
Is pass by pointer also a method of pass by reference?
Technically, all arguments to functions in C are pass by value. The language doesn't have support for true references unlike C++.
Passing by reference can be emulated in C by passing (by value) the address of the variable you want to modify, then subsequently dereferencing that address to modify the pointed-to value. This is not true pass by reference, however.
Pointers in C are references. C 2018 6.2.5 20 says:
… A pointer type describes an object whose value provides a reference to an entity of the referenced type…
When you pass a pointer to a function, you are passing the function a reference to whatever object the pointer points to. The pointer itself is passed by value, but, because the pointer is a reference and you are passing it, the description that you are passing an object by reference is accurate.
Prior to the development of C++, there was no dispute about this; people said they were passing an object by reference to describe passing a pointer to the object. C++ adopted the word “reference” as a name for a new feature in its language, so, in C++ terminology, “reference” generally refers to that feature unless otherwise stated or made apparent by context. However, in C, we are not obliged by C++ terminology, and the original meaning of pass by reference remains accurate.
In general, all arguments to functions in 'c' are passed 'by values'. It means that If an argument is modified in a function, this modification will not be visible by the caller:
void foo(int val) {
...
val = newval;
...
}
void bar() {
foo(4);
}
The value val in the above example can be modified and used in 'foo' but 'bar' will not see the update.
In order to make an update visible in the caller, pointers are used.
void foo(int *arg) {
...
*arg = 5;
...
}
void bar () {
int x = 4;
foo(&x);
// x will be 5 here
}
In the above case the address of 'x' will be passed to the function foo and it will be used inside the function *arg to modify the value of 'x'.
One can modify the value of the pointer inside the foo, since it itself is passed by value
void foo(int *arg) {
arg = newAddress;
}
but function 'bar' will not see this modification as well.
So, there are no real 'references' in 'c', however some people use it in reference to the passing of pointers.

Function that returns a multidimensional array

I've a function like this (in a file file_name.c):
char function_name(multi_array[][10])
{
/*change some character of multi_array*/
return multi_array;
}
That takes multi_array, a multidimensional array of characters, changes some characters of the given parameter, and than returns multi_array modified.
In main.c, i call the function like this:
multi_array_in_main = function_name(multi_array_in_main);
But the compiler gives me an error "icompatible type char[10][10] from type char"
What should i do? I'm not very confident with C so i don't know..!
You don't need to return anything.
Change:
char function_name(multi_array[][10])
To:
void function_name(multi_array[][10])
And your code should work fine (function_name will update whatever array it receives as an argument, as long as the dimensions are correct).
Change the function to return void and remove the return statement. The array is actually passed as a pointer to it's first element, so any changes you make to it inside your function actually change the original object in the caller.
void function_name(multi_array[][10])
{
/*change some character of multi_array*/
}
In your function header you declare function to return "char" type, but you return variable of char [][10], which is different type from the one in declaration (first line of your code).
Solution depends on what you really want to do. If you want to return that multiarray, change your function declaration. Also you defined parameter to be array of arrays, but it must be "array of array of char". Long story short, your declaration line should probably look like this:
char[][] function_name(char multi_array[][10])
Also, the changes made in multi_array made by this function will change multi_array even "outside" of the function and therefore you dont really need to return it. So you probably want to write this:
void function_name(char multi_array[][10])
As said, you do not need to return anything. The array is not copied, it is passed to your function as a pointer to the first element of the array. So, any element you change inside the function will be changed also outside because there is only one unique array.
Also if you insist, theoretically, it is possible to define a function returning a pointer to an array which is the closest thing to your original post. The declaration would be:
char (*function_name(char multi_array[][10]))[10] {
...
return(multi_array);
}
It is so ugly, that you will probably prefer to define a new type for it:
typedef char (*multi_array_t)[10];
multi_array_t function_name(multi_array_t multi_array) {
...
}

confusing C syntax

I am reviewing some C code, but having a hard time understanding what Callback is exactly. Does anyone know what this means? I'm guessing that it is defining "Callback and x to be both a void *?
typedef void (*Callback)(bool x);
It makes a new type name Callback. Every Callback will be a pointer to a function taking a bool and returning void. In effect Callback will be an alias for that real type. So when you say:
Callback ptr = some_fun;
You're making a function pointer that points at some_fun. Function pointers are typically passed to other functions as arguments.
It declares a function pointer type by the name Callback which points to a function which takes a bool input parameter and returns a void.
Once you specify the statement, You can use Callback as a type to hold address of a function with the specifed type.
Refer the Clockwise spiral rule when in doubt.

Pointer to function in ROM

I have microcontroler that I am working with. When debugging it is necessary to call a function from that is hard coded in ROM. Technical Reference shows how to do this:
# define Device_cal (void(*)(void))0x3D7C80
and calling procedure looks like this:
(*Device_cal)()
I can't understand what actually happens here, so my question is:
How does it work?
void (*) (void) is a type. It's a pointer to a function that takes no parameter and returns void.
(void(*)(void)) 0x3D7C80 casts the 0x3D7C80 integer to this function pointer.
(*Device_cal)() calls the function.
(Device_cal)() would do the exactly the same.
The parentheses around *Device_cal and Device_cal are required because otherwise the cast to the integer would not have the higher precedence.
The #define causes (*Device_cal)() to be expanded into this immediately before compiling:
(*(void(*)(void))0x3D7C80)()
The void(*)(void) is a declaration for a function pointer that takes void and returns void types. The (*()) represents a cast for the next token in the expression (0x3D7C80). Thus this asks to treat the data at location 0x3D7C80 as a function. The final () calls the function with no arguments.
well, you "define" a pointer to function, and call it.
void(*)(void) mean a pointer to function, that gets no arguments, and return void.
If you cast 0x3D7C80 to that type, and call it, you basically call the function that its address is 0x3D7C80.
This is not an answer (that has already been done satisfactorily), but some advice:
I would suggest the following method instead:
typedef void (*tVOID_ROMFUNCTION_VOID)( void ) ;
tVOID_ROMFUNCTION_VOID Device_cal = (tVOID_ROMFUNCTION_VOID)0x3D7C80 ;
Device_cal() ;
That way you can create any number of global function pointers on initialisation while the calls look like normal statically linked functions. And you avoid confusing pre-processor macros voodoo at the same time.
By creating different function-pointer types with different signatures, the compiler will be able to perform some parameter type checking for you too.
The symbol is pasted in which creates a temporary (un named ) pointer to a function at a fixed memory location and then calls it via dereferencing.

Resources