Ristricting function calls in C

Jul 26, 2011 at 9:06pm
Suppose I have a structure S containing a function pointer. Now we want this function to be called only by the structure containing pointer only.Not from main directly. How can we design a scheme like that any ideas ??

Jul 26, 2011 at 9:41pm
we want this function to be called only by the structure containing pointer only
Can you elaborate of what you mean by this please.
Jul 26, 2011 at 9:45pm
Now we want this function to be called only by the structure containing pointer only.

All function in a struct exclusively the only one to have access this function pointer? In C?
Jul 27, 2011 at 6:05pm
suppose there is a function

int sum (int x,int y){ return x+y;}

And we have a structure s { int a ; int b; int (*p)(int ,int)}

I want to design such a scheme such that we are able to call sum through the struct object.

s obj;
obj.p = sum
p(10,5) ; // valid.

But in general a call to sum(10,5) should give a error. How can we design such a scheme ?

Any Ideas
Jul 27, 2011 at 11:07pm
You could have some initialisation function that sets up p to point to a (file) static function. That way, p would point to the right function and the funtion would be otherwise inaccessible to the rest of the program.
Jul 28, 2011 at 6:13am
Thanks for the reply, I tried doing this but I am getting errors. This is my code.

header file

typedef struct{
int v;
void (*p)(int);
}node;


1.c

#include "h.h"
static void show(int x){
printf("%d\n",x);
}

2.c

#include<stdio.h>
#include "h.h"

int main(){
node a;
a.p=show;

return 0;
}

when I compile this as gcc 1.c 2.c it says no reference to show found . Any help ?
Jul 28, 2011 at 3:48pm
In my eyes, this is simply not possible in C. In C++ you would just make the function pointer private, but since struct fields are all public, I see no way of hiding it.
Jul 28, 2011 at 5:13pm
Data hiding in C is typically done with use of a void pointer. This is effectly what FILE and similar structs do. You never access any FILE members directly (you can't). Instead you call fopen/fread/whatever, and those are what actually access the members.

It's accomplished with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
// mystruct.h
typedef struct
{
  void* privatedata;
} MyStruct;


// Creating / Destruction
MyStruct* MyStruct_Create();
void MyStruct_Destroy(MyStruct* p);

// a function that uses MyStruct:
void MyStruct_Foo(MyStruct* p);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// mystruct.c

#include "mystruct.h"

typedef struct 
{
  int foo;  // actual data members of MyStruct
} MyStruct_Data;

// create a MyStruct
MyStruct* MyStruct_Create()
{
  MyStruct* p = malloc(sizeof(MyStruct));
  p->privatedata = malloc(sizeof(MyStruct_Data));
  return p;
}

// destroy a MyStruct
void MyStruct_Destroy(MyStruct* p)
{
  free(p->privatedata);
  free(p);
}

// a function that uses MyStruct
void MyStruct_Foo(MyStruct* p)
{
  MyStruct_Data* dat = p->privatedata;

  dat->foo = whatever;
}



Since the actual data members are in the c file and not in the h file... only functions in the c file are able to access them. Therefore code outside of MyStruct can only interface with it through the provided functions.
Jul 29, 2011 at 5:34am
Sorry about not replying sooner, I can't post code samples from my current day job.

This is sort of what I had in mind.

op.h
1
2
3
4
5
6
7
8
9
10
#pragma once

struct binary_op
{
	double arg1, arg2;
	double (*f)(const struct binary_op* arg);
};

void init_add(struct binary_op* arg);
void init_mul(struct binary_op* arg);


op.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "op.h"

static double add(const struct binary_op* arg)
{
	if (!arg)
		return 0.0;

	return arg->arg1 + arg->arg2;
}

static double mul(const struct binary_op* arg)
{
	if (!arg)
		return 0.0;

	return arg->arg1 * arg->arg2;
}

void init_add(struct binary_op* arg)
{
	if (arg)
		arg->f = add;
}

void init_mul(struct binary_op* arg)
{
	if (arg)
		arg->f = mul;
}


main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "op.h"
#include <stdio.h>

int main()
{
	struct binary_op arg = { 2.0, 3.0 };

	init_add(&arg);
	printf("%f + %f = %f\n", arg.arg1, arg.arg2, arg.f(&arg));

	init_mul(&arg);
	printf("%f + %f = %f\n", arg.arg1, arg.arg2, arg.f(&arg));

	return 0;
}


output
1
2
2.000000 + 3.000000 = 5.000000
2.000000 + 3.000000 = 6.000000
Last edited on Jul 29, 2011 at 5:36am
Topic archived. No new replies allowed.