This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 7 years ago.
help me to understand the copest of pointers to function with this following example .
i was referring balagurusamy book of 'c' but was unable to understand this concept
i also googled but didnt understood the concept
here is the code
#define PI 3.14
double y(double);
double cos(double);
double table (double(*f)(),double,double,double);
main()
{
printf("table of y(x)=2*x*x-x+1\n\n");
table(y,0.0,2.0,0.5);
printf("\ntable of cos(x)\n\n");
table(cos,0.0,PI,0.5);
}
double table(double(*f)(),double min,double max,double step)
{
double a, value;
for(a=min;a<=max;a+=step)
{
value=(*f)(a);
printf("%5.2f %10.4f\n",a,value);
}
}
double y(double x)
{
return(2*x*x-x+1);
}
if u could explain me in detail about the concept of "pointes to function" then it would be really very very helpful
The function is just a piece of machine code. Machine code is just bytes of data coding instructions for CPU. That is, on the lowest level, the code and the data are not fundamentally different. So the concept of pointer to code is not too different from the pointer to data.
Related
This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.
This question already has answers here:
C: Extrapolating type from void pointer
(4 answers)
Closed 5 years ago.
How can I know which type(int, double, float etc..) is currently holding of void pointer?
Suppose
void *p;
int x=10;
p=&x;
printf("%s",type_of_void_pointer(p));
double d=1.5;
p=&d;
printf("%s",type_of_void_pointer(p));
The first printf should print "int" whereas second should print "double"
Is there any way to write type_of_void pointer function?
You can't know the type from the content in anyway. From the void* itself it is no way possible to know this. It is all address. Even if you look into the content it is impossible to know the type of it.
All you know here is the address and that's it. You can even cast it to different type and interpret it different way.
This question already has answers here:
Assign multiple values to array in C
(8 answers)
Closed 8 years ago.
This is my code
int numLeft[5];
void init()
{
numLeft = {5,4,3,3,2};
}
When I tried compiling this code, I got this error: "error: expected expression before '{' token.
I know in java something like this could work
int[] numLeft;
void init() {
numLeft = {5,4,3,3,2};
}
Is there something i am missing in my C code? A quick google search isn't helping.
int numleft[5] = {5,4,3,3,2}
is the way to go. Here numleft is the variable that is referring to the array, but to access each, you've to refer to it as numleft[0], numleft[1], likewise.
int numLeft[5] = {5,4,3,3,2}; wiil do the job
if you need to initialize the global array each time, need to use memcpy(), as suggested in the other duplicate answers.
This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 8 years ago.
i am trying to understand what is the difference when using structs and typedefs to access some components
what is the difference between using the dot operator when dealing with structs
using the example below
so far i have tried this code
typedef struct _game{
int something;
char something_else;
} g;
if i use
g.something or g->something
what is the difference?
i have used both of them and they both return results but i still dont understand the difference
can somebody please explain this?
I'm assuming this is C. When asking language questions tag the language. There are many languages that look the same and can give you subtly different answers. C++ is a different language than C, btw.
In this statement,
typedef struct _game { int something; } g;
g is a type, not a variable. As such, g.something makes no sense. typedef means "type define". Instead, you would have
g my_g_instance;
g *my_g_ptr = &my_g_instance;
my_g_instance.something = 2;
my_g_ptr->something = 5;
The difference between . and -> is whether the variable to the left of the operator is a pointer or not.
This question already has an answer here:
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 9 years ago.
void test(int x[static 10]);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11};
test(a);
return 0;
}
void test(int x[static 10])
{
printf("%d",x[9]);
}
I was looking for bizarre C statements. I found this one, But could not understand what is the use of static 10 in that statement. Is it same as int x[10]?
Another thing, you can use volatile also, in place of static e.g int x[volatile 10]
Anybody knows what is the use of this kinda declaration?
PS: Compiled using GCC 4.6.3,
It's a hint for the compiler telling that the x pointer parameter points to the first element of an array of at least 10 elements.
For example:
test(NULL); // undefined behavior