How to access value passed into void tmin(void) function and then alter it? - c

I am working on an assignment. My teacher has given me this function
void tmin(void){
return 2;
}
I need to take whatever value is passed into this function, manipulate it, and return it. Where I am stuck is the tmin(void) part. How do I work on the value that is passed in if it just says void? Is there a way to assign it to a new variable?

A function void f(void) { ... } does not accept a anything to pass into the function, nor does it return anything. You need to change the signature of the function (return type and parameters and its types) and its body according to the task your teacher gave to you.

Related

How to call a single argument from a function with multiple argument in C?

Say you have a several function like this:
void Inventory(int index, char input[], int qty)
void AddItem(){
int index = Inventory(index);
if (int i = 0; i < index; i++){
...
}
}
But it gave me an error 'A value of type "Void" cannot be used to initialize an entity of type "int"'
Can someone explain to me in detail since im new to programming too.
You're trying to initialize index as an int whose value is returned by calling Inventory(index). But the Inventory function you provided has a return type of void, not the expected int, so there's no way to get that value.
Also, your call to Inventory is missing an argument to the chat input[] and int qty parameters. Additionally, index is uninitialized at the time that you're trying to use it (within the definition of index).
The function Inventory does not return anything (which is void) and you are trying to affect 'nothing' to a variable of type int. That's why the compiler is complaining.
The solution is to have your Inventory function return an int value instead of void.
First of all, your question and code snippet that you have provided are misleading.
Secondly, You are getting this error because the return type of function Inventory is Void, which means it returns nothing. And you are trying to assign nothing to variable index, which is of type int.
And, if you are trying to call multi-argument function without having to pass all arguments, make rest of the argument optional.
void foo(/.../) means that the function foo does not return anything. So you cant assign nothing to something.
In C you need to pass all the parameters. In other languages (like C++) some parameters might have default values and you do not have to pass them when call the function. But it is not possible in the C language and you need to pass all the parameters (arguments)

How to call a Void function which has a void parameter in C

I am a beginner in C.
For now I have a function like this
void mark_function(void *obj, void (*mark_obj)(void *));
To my understand, this a void function, and it has two parameter, first one is a void pointer,
and the second one is a another void function with a void parameter.
I tried a lot ways to call it, but it seems not work properly,either give me back the segmentation false or the pointer type not the same warning.
So What exactly means for this function? what parameters should pass in? and how to call it?
Any help?
Thanks alot!
Let's make it more clear first. You have a void function, called mark_function, which takes 2 parameters. The first parameter is a void pointer and the second parameter is a pointer to a function that returns void and takes as a parameter a void pointer. Let's create a function that will be apropiate to pass as parameter to mark_function.
void param(void *p) {
// function body
}
Assume a and b are 2 void pointers. I will not enter into details about their scope, but you need to pay attention to it: they must be available in the scope they are used.
Then, the mark_function will be called as:
mark_function(a, param);
Inside mark_function body you can have something like:
param(b);
, which is a call to the function passed as a parameter.
Long story short: the function pointers used as parameters are meant to make it possible for the function that requires them to perform different activities by calling different functions. The value of a pointer function is simply the name of a function that has the appropiate signature (return value and parameter list). Your function can use this to call the function provided as parameter as needed.
You should call the function with one pointer to an object, and one pointer to a function of suitable type.
int my_int;
void mark_function(void *obj, void (*mark_obj)(void *));
void my_func (void *vp)
{
/* Convert vp to a pointer to a real type */
int *ip = vp;
/* Do something with ip or *ip */
}
mark_function (&my_int, my_func);
A pointer to object (&my_int) can safely be convert to a void * and back to the same type. The resulting pointer is guaranteed to be identical to the original pointer. This allows mark_function() to perform some task regardless of the actual type that the pointer points to.
Typical examples of such tasks include sorting of arrays with a custom compare function provided by the caller, or maintaining linked lists without knowing the types of the object stored inside, an so on.

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) {
...
}

Passign code as a function parameter instead of declaring a callback

I think that some newer languages like JS can do this natively, but I forget the term for it (make a "temporary" function in-line just to pass as a callback)
What I want to do is ...
I'm writing unit tests where I set up expected input & output messages at compile time. Later at run time, I want to do some checks when each output is received or input has been processed, so I added a parameter for a callback function.
That's working fine and I could leave it & move on, but ... I am just curious ...
sometimes a function is overkill and I just need a single comparison; sometime a small block of code would do. Perhaps I could just evaluate these to a zero/non-zero value at run time? But how to pass as a parameter?
At the moment my function has the following signature
void AddExpectedCommand(E_peripheralType peripheral,
communicationBlock_t commandBlock,
errorMessage_t errorMessage,
void *(*DoRunTimeChecks)(E_boolean));
where the final parameter is pointer to a callback function returning boolean.
Is there any way that I could pass a code expression as a parameter instead?
Or does a function seem "cleaner"?
Thanks in advance for any help ...
Update: oops, I got my declaration wrong. I want to pass a pointer so a function which has no parameters and returns an e_Boolean ... how do I do that?
With C++11 you can do the following:
Function taking a function that returns a bool:
void f(function<bool()>);
Call it with a lambda capturing a local variable:
int x = ...;
f([&x]() { return (x > 2); });
Or call it with some function g that returns a bool:
bool g();
f(g);
Or bind some function h that takes an int and returns a bool:
bool h(int x);
f(bind(h, 2)); // ie creates a nullary function from h(2)
What you're looking for is called a "lambda expression" or "anonymous function." And they don't exist in C (but do in C++ with certain qualifications).

Function pointer with default parameter in C

See, I have two functions, one to get a char, and other to put a char like this.
void function_get_char (input);
void function_put_char (output);
I have a function pointer to these functions like this.
void (*function_operators[])(input_or_output) = {function_get_char, function_put_char};
I need of, when I'll call my function_operator, I want to don't need to specify if I want to get it in of output or of my input.
Do I need to build my function pointer like this?
void (*function_operators[])(input_or_output) = {function_get_char(input), function_put_char(output)};
How can I do it?
Thanks in advance.
NOTE
input or output parameter, is not run_time parameter.
I'm not really sure what you want to do, but if you want to fix input and output to some predefined value (since you say they are not runtime parameters), the easiest solution should be to write some wrapper functions that call the "real" functions with the correct parameters. For example:
void fixed_get_char(void) { function_get_char(input); }
void fixed_put_char(void) { function_put_char(output); }
void (*function_operators[])(void) = {fixed_get_char, fixed_put_char};
You will have to create a function with no arguments which wraps the call to the function with an argument. If you need to do this at runtime, then look at (one of the definitions of) thunk or trampolines.

Resources