where location to assign function pointer point to a function - c

I have a funtion_ptr function pointer which point to add_int function.
case 1: when lay a statement function_ptr = &add_int outside main function
--> compiler error: error C2373: 'function_ptr' : redefinition; different type modifiers (this is in )
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int);
function_ptr = &add_int; // it's here
void main(){
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
_getch();
}
case 2: function_ptr = &add_int; in main function --> it is true
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int);
void main(){
function_ptr = &add_int; // it's now here
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
_getch();
}
Could anyone explain for me different between the two case.
Thanks!

function_ptr = &add_int; is an assignment statement. Statements are allowed inside functions, but outside functions only declarations are allowed. Since assignment is not a declaration, the compiler issues an error.
If you want to assign the pointer as part of its declaration/definition, you could combine the declaration and the assignment, like this:
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int) = &add_int;
int main(){
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
return 0;
}
Demo.

You don't need the & in the assignment statement, the function's name (add_int) IS its address... and the assignment statement should be within the bounds of a function.
Here's an example that works:
#include <stdio.h>
int add_int(int n, int m);
int(*function_ptr)(int, int);
void main(){
function_ptr = add_int;
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
getc(stdin);
}
int add_int(int n, int m){
return n + m;
}

Related

f(&a) is possible to run in C?

The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.

Pointer to pointer as a function parameter

I have the following code:
#include <stdio.h>
#include <stdlib.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
int main(void) {
int c = 4;
printf("f(): %d\n", f(c, &c, &&c));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
How can I access **ppz correctly, because so I get an error message: "label 'c' used, but not defined".
An int** is a pointer to an int*. You need to create a variable of type int* to be able to pass a pointer to it somewhere. Here is what you should do:
int main(void) {
int c = 4;
int* pc = &c;
printf("f(): %d\n", f(c, pc, &pc));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
Refer #ikegami's answer for an explanation of the proper use of a pointer to a pointer.
You want to modify a variable of type int, so the parameter should be int *, not int **.
You'd use int ** if you wanted to modify a variable of type int * variable. That's not the case here.
For example,
void f(int **pp) {
*pp = malloc(10);
}
int main(void) {
int *p;
f(&p);
// ...
free(p);
}

C Struct Function Syntax - Pass elements, return struct

I'm trying to create a function that accepts an array and two integers to manipulate and return in a struct.
What i have looks like this:
#include <stdio.h>
struct Results {
int *A; // Pointer para o Array
int N; // Comprimento do Array
};
int k, n;
struct Results solution(int A[], int N, int K);
int main(void){
int a[] = {1,2,3};
struct Results out;
k = 1;
n = sizeof(a)/sizeof(a[0]);
printf("n = %d \n", n);
out = solution(int a[], int n, int k);
// EXPECTED EXPRESSION !!
}
struct Results solution(int A[], int N, int K) {
struct Results outp;
outp.A = A;
outp.N = N;
return outp;
};
I can't pass from this point, the compiler tells me that a expression is expected when I declare the function.
I think this might be a basic syntax error...
You are mixing the syntax for defining or declaring a function with the actual call to that function:
out = solution(a, n, k);
The types of the parameters are only present in the prototype, not in the call.

Can you really redefine keywords in C language?

Can you explain the code below? How we can use #define for a keyword of C?
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
Output:
10 5
This #define int int* is a preprocessor macro. If you want to define your own synonyms for types then use typedef. You cannot create keywords in a language you did not create.
Sample:
#include <stdio.h>
typedef int * myIntPtr;
int main(void) {
int i = 10;
myIntPtr x = &i;
printf("%d", *x);
return 0;
}
Output:
10
Also semantically making an int to an int * makes no sense.
#define a b
will change all "a" to "b" in your source after the #define
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
will change to
int main(void) {
int **p;
int *q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}

how to make a array of pointer to call func pointer?

i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.

Resources