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});
Related
This question already has answers here:
Currying/binding with ISO C99
(5 answers)
Functional Programming (Currying) in C / Issue with Types
(4 answers)
Is there a way to do currying in C?
(6 answers)
Partially applying a function in C
(3 answers)
Closed 3 years ago.
let's assume this variadic function is printing nice logs (without knowing its implementation because we don't care)
int printLog(int bla, int foo, const char *format, ...);
in the following code how do I affect the arguments?
void myFunction(int param)
{
typedef int (*myPrint) (const char *, ...);
if(param > 0)
{
myPrint = printLog; //how do I pass half of the argument here?
}
else
{
myPrint = printLog(1,2,...); //this is what I would like
}
//then use it
myPrint("Hello World");
unsigned int blah=1;
myPrint("blah is %d",blah);
}
This question already has answers here:
what is the default value of char pointer or char variable [duplicate]
(5 answers)
Closed 4 years ago.
I am running my code with gcc. I have a function in which I declare a variable X1 which is initialized to 'inf'.
function(double nu, void *params) {
struct func_params *part= (struct func_params *)params;
double result;
*commands*
if (condition){
double wb,X1;
printf("inside if X1 %e \n",X1);
}
return result;
this code is returning "inside if X1 inf". I never had that issue and I didn't change anything to the code...Any idea what it could be?
It is Undefined bahavior and your unitialized variable can have any value including inf & NaN.
When you use it you invoke the Undefined Behavior
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.
This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
Closed 7 years ago.
Using typedef for functions, like in the following example, are there any differences between using the address of the function or only the function?
#include <stdio.h>
int foo(int i){return i+1;}
typedef int(*mytype[2])(int);
int main(void)
{
mytype f;
f[0] = foo;
f[1] = &foo;
printf("%d %d", f[0](5), f[0](6));
return 0;
}
In the C language, functions are implicitly converted to function pointers in most contexts. Thus you see no difference between f[0] = foo and f[1] = &foo. I prefer the latter convention, but really, both are equally fine.
This question already has answers here:
Changing value of const int using using pointer [duplicate]
(2 answers)
Closed 8 years ago.
I got a program crash when I am trying to modify constant variable through a pointer.
#include <stdio.h>
static const int x = 5;
void changeX(int *x)
{
(*x) = 20;
printf("%d", (*x));
}
int main(void)
{
printf("Jelele");
changeX((int *)&x);
return 0;
}
I know it isn't a good practice and there is no need to make such that ... I am just testing something ...
My question is:
Why program crashes ?!
static const int x = 5;
This is a constant variable stored in read-only location so when you try to write to this location then you see a crash.
Like
(*x) = 20; /* This is UB */
Check the below link:
Where are constant variables stored in C?
You could change where the pointer x points to, but as the integer x is constant, its value cannot obviously be changed by definition.