473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a function using its pointer-

Hi
I have declared a structure that contains pointer to a function. The
code goes like this:
typedef struct lcd_funs lcd_funs;
struct lcd_funs {
void (*decode_image) (cyg_uint32 imageWidth,
cyg_uint32 imageHeight,
Palette_element *paletteData,
cyg_uint8 *imageData,
cyg_uint16 bitsPerPixel
);
};

Now, suppose, I have a variable funs that is pointer to a structure of
this type. When I try to call this function using the code below, it
hangs up. Any idea why it is happening like this? The code used to
call it as follows:
(funs->decode_image)( ptrConfigOption s->imageWidth,
ptrConfigOption s->imageHeight,
ptrConfigOption s->paletteData,
ptrConfigOption s->imageData,
ptrConfigOption s->bitsPerPixel
);
Is it the right way to call functions using their pointers that are
members of a structure?

Regards
Samie
Nov 14 '05 #1
4 1566
Samie <sa*****@hotmai l.com> wrote:
I have declared a structure that contains pointer to a function. The
code goes like this:
typedef struct lcd_funs lcd_funs;
struct lcd_funs {
void (*decode_image) (cyg_uint32 imageWidth,
cyg_uint32 imageHeight,
Palette_element *paletteData,
cyg_uint8 *imageData,
cyg_uint16 bitsPerPixel
);
}; Now, suppose, I have a variable funs that is pointer to a structure of
this type. When I try to call this function using the code below, it
hangs up. Any idea why it is happening like this? The code used to
call it as follows:
(funs->decode_image)( ptrConfigOption s->imageWidth,
ptrConfigOption s->imageHeight,
ptrConfigOption s->paletteData,
ptrConfigOption s->imageData,
ptrConfigOption s->bitsPerPixel
);
Is it the right way to call functions using their pointers that are
members of a structure?


Yes, but you don't need the parentheses around the 'funs->decode_image '
bit. Why it hangs is a different question. First possibility to get
it wrong is not assigning the correct pointer to the 'decode_image'
member of the structure. Second possibility is that the function isn't
working as you expect it to. Did you try to use a debugger to see if
the function gets called at all (e.g. by setting a breakpoint at the
very start of it) and then to see what happens inside of it?

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
Samie wrote:
Hi
I have declared a structure that contains pointer to a function. The
code goes like this:
typedef struct lcd_funs lcd_funs;
struct lcd_funs {
void (*decode_image) (cyg_uint32 imageWidth,
cyg_uint32 imageHeight,
Palette_element *paletteData,
cyg_uint8 *imageData,
cyg_uint16 bitsPerPixel
);
};

Now, suppose, I have a variable funs that is pointer to a structure of
this type. When I try to call this function using the code below, it
hangs up. Any idea why it is happening like this? The code used to
call it as follows:
(funs->decode_image)( ptrConfigOption s->imageWidth,
ptrConfigOption s->imageHeight,
ptrConfigOption s->paletteData,
ptrConfigOption s->imageData,
ptrConfigOption s->bitsPerPixel
);
Is it the right way to call functions using their pointers that are
members of a structure?


The call looks all right to me. Have you actually
set the `decode_image' element to point at a function
of the appropriate type?

void decode_jpeg_ima ge( /* prototype here */ );
void decode_tiff_ima ge( /* prototype here */ );
...
if (jpeg_rulez)
funs->decode_image = decode_jpeg_ima ge;
else
funs->decode_image = decode_tiff_ima ge;
...
funs->decode_image ( /* argument values here */);

If `decode_image' is in fact pointing to the function
you expect (you could insert a printf() at the start of
that function to be sure you get there), then your "it
hangs up" problem has some other cause.

--
Er*********@sun .com

Nov 14 '05 #3
before you use the call funs->decode_image,y ou must do this as follow:
funs=(lcd_funs *)malloc(sizeof (lcd_funs));

Samie <sa*****@hotmai l.com> wrote in message
news:6b******** *************** ***@posting.goo gle.com...
Hi
I have declared a structure that contains pointer to a function. The
code goes like this:
typedef struct lcd_funs lcd_funs;
struct lcd_funs {
void (*decode_image) (cyg_uint32 imageWidth,
cyg_uint32 imageHeight,
Palette_element *paletteData,
cyg_uint8 *imageData,
cyg_uint16 bitsPerPixel
);
};

Now, suppose, I have a variable funs that is pointer to a structure of
this type. When I try to call this function using the code below, it
hangs up. Any idea why it is happening like this? The code used to
call it as follows:
(funs->decode_image)( ptrConfigOption s->imageWidth,
ptrConfigOption s->imageHeight,
ptrConfigOption s->paletteData,
ptrConfigOption s->imageData,
ptrConfigOption s->bitsPerPixel
);
Is it the right way to call functions using their pointers that are
members of a structure?

Regards
Samie

Nov 14 '05 #4
Buzzard <Bu*****@126.co m> wrote:
Samie <sa*****@hotmai l.com> wrote in message
news:6b******** *************** ***@posting.goo gle.com...
Hi
I have declared a structure that contains pointer to a function. The
code goes like this:
typedef struct lcd_funs lcd_funs;
struct lcd_funs {
void (*decode_image) (cyg_uint32 imageWidth,
cyg_uint32 imageHeight,
Palette_element *paletteData,
cyg_uint8 *imageData,
cyg_uint16 bitsPerPixel
);
};

Now, suppose, I have a variable funs that is pointer to a structure of
this type. When I try to call this function using the code below, it
hangs up. Any idea why it is happening like this? The code used to
call it as follows:
(funs->decode_image)( ptrConfigOption s->imageWidth,
ptrConfigOption s->imageHeight,
ptrConfigOption s->paletteData,
ptrConfigOption s->imageData,
ptrConfigOption s->bitsPerPixel
);
Is it the right way to call functions using their pointers that are
members of a structure?

Please don't toppost.
before you use the call funs->decode_image,y ou must do this as follow:
funs=(lcd_funs *)malloc(sizeof (lcd_funs));


Not necessarily. If the structure is defined and initialized e.g. in
some function and then a pointer to it is passed to some other function,
where funs->decode_image () is called, no dynamic allocation is required.
BTW, even if an allocation would be needed the cast of the return
value of malloc() is superfluous, it will just keep the compiler from
complaining if you forgot to include <stdlib.h>.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #5

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

Similar topics

4
2422
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage collection is not used in this class. (singleton class + override InitializeLifetimeService ) The service class uses C++ unmanaged function from dll (using DLLimport).
12
2453
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone give me some advice? I don't know where to go to look this information up. Do I need to access the stack, and hence do some kind of inline assembly code to get the calling address?
8
1617
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
5
3026
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
1
2597
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
4
3332
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias "CM_Get_Device_ID_List_SizeA" (ByRef pulLen As Integer, ByVal pszFilter As Integer, ByVal UlFlags As Integer) As Integer - To get the length of the device list. This seems to work as I get a CR_SUCCESS (I get a number around 8500. But as I'm not sure what is in the
2
3143
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
18
4342
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
3
4702
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
10
6965
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
8399
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
8827
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
8732
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
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
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...
0
5632
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
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.