How to call a single argument from a function with multiple argument in C? - 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)

Related

What types of prototypes exist in C?

Well, i read somewhere (i dont remember where) that are like types of prototypes in C. Ones used to sending parameters and others used for data return. What are those named and what are those used for ?
"...Ones used to sending parameters and others used for data return...."
Using common terms, you may be referring to function parameter types, in particular as they have to do with input parameter and output parameter.
In actuality, C does not distinguish or define input and output function parameters per se. By definition C has only pass by value function parameters. However, the type of value that is passed can be one of two distinct categories: either the value represented by the object itself, or the value of the address of the object. If it is the value of the object itself that is passed, then the called function is not able to modify that value. If however the value of the address of the object is passed, then the function is able to modify the value of the object residing at that address, and upon function return, the updated value of the object is accessible via the return function parameter.
So, to simplify wording for this illustration, and in the context of this answer the following terms are used:
input - function parameter that passes value of object.
output - function parameter that passes the value of the address of the object and provides access to updated object value upon return.
To illustrate, with respect to input and output the following prototype has both. The first two arguments are input, and the third is output.:
void func1(int val1, int val2, int *sum)
{
*sum = val1 + val2;
}
called like this:
int a = 100;
int b = 300;
int sum = 0;
int return = func1(a, b, &sum);//value of sum == 400 upon return
There is only one type of function prototype. Using the function prototype you tell the compiler what is the type of return value of the function and how many and what type arguments are.
Historically functions can have default type of arguments and default type of the return value. The default type is int

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

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.

Calling another function from printf?

I am doing a simple challenge from a book on C/Objective-C where I have to write a program that computes and displays the square of an integer, while also putting the numbers in quotation marks.
I made a program that works successfully but I have two questions about it.
Firstly having had the the idea of cutting back on the amount of code in mind, to practice being efficient and concise in my code, I called my squareOfInteger function from my printf statement. Is this advisable though? It worked but I'm worried this might be bad practice?
The other thing nagging at me is whether or not I need to reiterate the type as being an int inside the parameter of my function before my variable number, or whether it is enough to declare the result as being of type int, and leaving out type in the parameter, which I have done below.
Here is my code:
#include <stdio.h>
int squareOfInteger(number)
{
int square = number * number;
return square;
}
int main(int argc, const char * argv[]) {
int myNumber = 5;
printf("\"%d\" squared is \"%d\".\n", myNumber, squareOfInteger(myNumber));
return 0;
}
I would greatly appreciate your help.
I called my squareOfInteger function from my printf statement. Is this
advisable though? It worked but I'm worried this might be bad
practice?
Calling a function from printf() is not bad. But there might be scenarios leading to undefined behavior since the order of evaluation within printf() is not specified.For example:
int func(int *p)
{
*p =30;
}
int main()
{
printf("%d %d",num,func(&num));
return 0;
}
Here you are calling func() as well as printing the value of num so you have UB.
whether it is enough to declare the result as being of type int, and
leaving out type in the parameter
If the type of the argument is not specified then it defaults to int.But it is always good to mention the type of the arguments.
"I called my squareOfInteger function from my printf statement. Is this advisable though?"
There is nothing wrong in calling the function from within a function. It depends on case, where you say need to retain the squared value (as not in your case where you just need to print the value) and perform other operations on it. like:
int value = myFun(100);
int nextVal = myNextFun(value);
"The other thing nagging at me is whether or not I need to reiterate the type as being an int inside the parameter of my function before my variable number, or whether it is enough to declare the result as being of type int, and leaving out type in the parameter, which I have done below."
Yes you should use define the type of number (in your case as by default it would be integer type and would fail if you have it of other type) as a best practice as it will make code more clear and readable.

Implicit declaration of function is invalid in C99

I am new to C language and I am having a problem that I really don't understand. I am trying to get an array from another function but when I try to extract the information, it gives me the following warning:
Implicit declaration of function 'getk_vector_calculation' is invalid in C99
Array initializer must be an initializer list or string literal
Here is the code:
int k_vector_calculation(int krec[3])
{
...
krec [0] = l*u[0]+m*v[0]+o*[0] ;
krec [1] = l*u[1]+m*v[1]+o*[1] ;
krec [2] = l*u[2]+m*v[2]+o*[2] ;
return k_vector_calculation( &krec[3] )
}
int main ()
{
char krec[3] = getk_vector_calculation(&krec[3]);
...
}
in your main() the function you called is getk_vector_calculation() [which is not k_vector_calculation()] and which is not declared or defined before the usage.
To resolve this,
either #include the header file containg the declaration of getk_vector_calculation() in your sorce file. [Considering getk_vector_calculation() is in some other file]
or, add a forward declaration of getk_vector_calculation() before main() and define getk_vector_calculation() somewhere.
To know more about implicit declaration, you can check this question.
EDIT:
As others have pointed out, there are many more problems in your code snippet. But since the question title is limited to implicit declaration error, IMO, this answer should do the needful. The remaining error(s) is(are) altogether a different aspect.
In older versions of C, functions that had not been declared yet were still able to be called, and it was assumed that they returned int and took an unspecified number of arguments. Strictly speaking, in C99, it is not permitted to call a function without declaring it first.
In your case however, you are trying to call a function called getk_vector_calculation but you have defined a function called k_vector_calculation (no get at the beginning).
You are also trying to initialise an array using a function, which is not permitted (in C, functions cannot return arrays). Simply declare the array and call k_vector_calculation as a separate statement, e.g.:
int krec[3] = {0};
k_vector_calculation(krec);
Don't use &krec[3] as this points to an invalid location. Use &krec[0] to provide the address of the first element in the array, or equivalently just krec will do. N.b. also that you declare an array of type char, but your function accepts a pointer to int, and these types are not compatible. Your function also calls itself unconditionally so there is a guaranteed infinite recursion if the snipped out code does not conditionally return. If your function doesn't need to call itself, and it doesn't return a value of any importance, change the return type to void to indicate it has no return value.
Since you are using C99, you can take advantage of using the static keyword in your function's parameter declaration:
void k_vector_calculation(int krec[static 3])
{
// ... other code here ...
krec[0] = l*u[0]+m*v[0]+o*[0];
krec[1] = l*u[1]+m*v[1]+o*[1];
krec[2] = l*u[2]+m*v[2]+o*[2];
}
The above code declares a function that takes as an argument an array of at least 3 int.
Several issues, here:
As Sourav Ghosh pointed out, you define k_vector_calculation(), but then try to call getk_vector_calculation(). You have to use the right names.
You say you want to "get an array from another function" - you just can't do this in C.
You don't show all your code for k_vector_calculation(), but as shown, this function will call itself forever, because the last thing it does is to unconditionally call itself again. If you have a recursive function, you need to give it a way to terminate.
&krec[3] is the address of the fourth element of the array k, which is not want you want to be doing, here, especially since your array only contains 3 elements. To refer to the array itself, just use krec.
char krec[3] = getk_vector_calculation(&krec[3]); is invalid for numerous reasons. One, you can't initialize arrays in this way in C. Two, see point (4) for your argument. Three, even if you could initialize arrays this way in C, you'd be trying to pass an uninitialized array to a function, initialize it in there, and then try to initialize your original array with the result. It just makes no sense.
You also write your functions to work with an array of int, but declare krec in main() as an array of char.
It's not clear what you want k_vector_calculation() to do, but you just can't return arrays in C like that. Probably what you want to do is just pass the array to the function, have the function work on in, and return nothing. For instance:
#include <stdio.h>
void k_vector_calculation(int kvec[])
{
kvec[0] = 1;
kvec[1] = 2;
kvec[2] = 3;
}
int main(void)
{
int kvec[3];
k_vector_calculation(kvec);
for ( int i = 0; i < 3; ++i ) {
printf("kvec[%d] is %d.\n", i, kvec[i]);
}
return 0;
}

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

Resources