This question already has answers here:
How to pass a constant array literal to a function that takes a pointer without using a variable C/C++?
(10 answers)
Closed 5 years ago.
So , i'm trying to make a simple program in C using arrays.
int odd(int v1[],int n) {
int v2[n];
int i;
for (i=0;i<n;i++) {
if (v1[i]%2==0) {
v2[i]=v1[i];
}
else {
v2[i]=v1[i]*2;
}
}
for (i=0;i<n;i++) {
printf("Array %d",v2[i]);
}
return 0;
}
int main() {
odd({1,2,3,4,5},5);
return 0;
}
I get an error in the main function ('Expected expression') and i don't know how to correct.
An expression like
{1,2,3,4,5}
is not an array, on it's own. It's a brace-enclosed list, which is primarily used for initialization purpose.
If you don't want to define a separate array variable, you need to make use of compound literal. Something like
odd((int[]){1,2,3,4,5},5);
will do the job.
Related
This question already has answers here:
Pointer to literal value
(12 answers)
Closed 4 years ago.
Given the following piece of C code:
void calc(int *value)
{
// do something with value
}
int main(void)
{
int i;
i = 10;
calc(&i);
}
Is it possible to get rid of setting up i and pass directly 10 to function calc? If yes, how can this be done?
Example of what I have in mind (which doesn't work):
calc( (int *) 10);
Nope, it's impossible to have a pointer to a constant in C.
UPD: however it seems that there is a trick using a struct compound literals syntax (thanks to #antti-haapala for the correction). Try this:
calc(&(int){10});
This question already has answers here:
Passing an array of structs in C
(9 answers)
Closed 6 years ago.
I want to know how I can send my array of structure to a function.
typedef struct {
char fname[20];
char lname[20];
int cnumber[12];
} contact;
contact record[40];
int main()
{
// I have all the data in the record array as I am reading it from the
// file and want to pass the record array to the function PRINT and access it.
print();
}
How can it be send in the function and print all the values using function call?
You can send your array of structures to a function like this:
void print(contact record[], int n) {
Then print the contents in this function and send it back to main() as:
print(record, n);
Note: the length of the array, n, should be kept track of somewhere in your program, then passed to print().
This question already has answers here:
How to achieve function overloading in C?
(14 answers)
Closed 6 years ago.
Let's assume I want to create a function first that returns the first element of an array in C. Obviously I want to create something that accounts for all types.
I would start with this:
int first(int list[]) {
return list[0];
}
Which works. Obviously...
I would now like to do the same for char
char first(char list[]) {
return list[0];
}
Which does not compile as there is already a first function in the program.
How do you C guys handle this kind of scenarios?
Is one forced to go with different names?
int first_int(int list[]) {
return list[0];
}
char first_char(char list[]) {
return list[0];
}
C11 introduced generic selections to emulate overloading:
#define first(X) \
_Generic((X), \
int* : first_int, \
char*: first_char \
)(X)
See it live on Coliru
This question already has answers here:
Passing an array as an argument to a function in C
(11 answers)
Closed 6 years ago.
When I pass in an array as a parameter in a function in C, does it create a copy of the array or does it actually make changes to the original array? I have a small program here that translates a number into its binary form. For example, if the number is 15, the binary form would be 1111.
void convertToBinary(int base10, int result, char *binaryResult){
int binaryVals[8] = {1,2,4,8,16,32,64,128};
if(base10 == 0){
printf("Binary Result %s", binaryResult);
}
else{
int max = 0;
for(int i = 0; i < 8; i++){
if(binaryVals[i] <= base10){
binaryResult[i] = '0';
}
else{
max = binaryVals[i-1];
binaryResult[i-1] = '1';
result = base10-max;
printf("Result %d", result);
break;
//convertToBinary(result,0, binaryResult);
}
}
}
}
int main(void){
char binaryResult[8];
convertToBinary(15,0,binaryResult);
}
The recursion part is failing me. I am not sure why. I suspect that it is because it is creating a copy of the array each time it runs the recursion. But I am not sure how to fix that. It would be great if someone could help, thanks
Arrays passed into functions always modify the original array. The function declarations:
void func( int* array);
void func( int array[]);
Are equivalent, because when you call these functions with an array argument the array decays into a pointer for both. Because you're passing a pointer, you're actually passing the array by reference, and the original array is modified.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are declarations put between func() and {}?
In C, what does it mean when I declare a variable following a function signature, before the function body?
Example:
int foo (i) int i {
printf ("the value of variable 'i' is: %d", i);
return i;
}
When I compile the the code in addition to initializing variable i, I get a compile error:
"cannot initialize parameter: p"
It means you are looking at old code.
That is the old K&R syntax.
Basically it says, i is the argument, and it is an int
You can rewrite it as
int foo (int i)
{
printf ("the value of variable 'i' is: %d", i);
return i;
}