473,790 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Return Function Pointer's Memory Address?

I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;

unsigned char* Get_foo(void)
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?

Bryan Parkoff
Sep 1 '05 #1
2 3118
Ben
Bryan Parkoff wrote:
I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;

unsigned char* Get_foo(void)
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?


Take a look here:

http://www.newty.de/fpt/fpt.html#r_value

Ben
--
I'm not just a number. To many, I'm known as a String...
Sep 1 '05 #2
Bryan Parkoff wrote:
I set Function Pointer variable to private so unauthorized users cannot
access it, but they are allowed to use Get_ and Set_ functions to modify
Function Pointer variable. Look at my example.

unsigned char foo(void)
Bad habit.
unsigned char foo()
{
return 0xC1;
}

unsigned char (*pfoo)(void) = foo;
unsigned char(*pfoo)() = foo;
unsigned char* Get_foo(void)
unsigned char(*)() Get_Foo()
{
// return pfoo; // Error -- can't return function pointer
return unsigned char (*pfoo)(void); // Error -- can't return function
pointer
}

Can you please correct Get_foo() function?

Sure.

The above works. But it is easier to write:
unsigned char foo()
{
return 0xC1;
}

typedef unsigned char (*pfoo_type)();

pfoo_type pfoo = foo;

pfoo_type Get_Foo()
{
return pfoo;
}

Gabriel
Sep 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
3
2407
by: Pushker Pradhan | last post by:
I have a function which should allocate memory, initialize it to some values and return the address of the initialized memory to the calling function. void getWaveletCoeffs(float *ld, float *hd, int *filterLen) { ld = (float*)malloc(2*SZFLOAT); hd = (float*)malloc(2*SZFLOAT); *ld++ = 0.7071; *ld-- = 0.7071;
4
157996
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
8
3273
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before opening to the file. I fail to write a function that allocates memory for a 2d array and returns it to main. I was trying to pass a pointer to the array back to main, but main cannot access the data. The simplified code (no opening of file, but...
23
3615
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
42
3172
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional array (size is 10*10), setting all the values inside the 10*10 matrix to zero. My C-function looks like this:
57
5676
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p expects type 'void *; but argument 2 has type 'void
4
2290
by: | last post by:
The output is: 1234 After getline: 1234 After renew: 1234 After retnp: İİİİİİİİİİİİİİİİG After getp: İİİİİİİİİİİİİİİİG -----What happen after renew/retnp call?------- Why not return a true array? -----The code is as followed------------------- #include <iostream.h>
13
13208
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
4
4403
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function pointer array instead of switch. They believe that ordinary function pointer is much faster than switch. Think of one variable called selectFunction. selectFunction variable can be the range of 0 through 1,000 or more. Load selectFunction...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.