0

What does the following statement do in C++?

(*JImageGetResource)(jimage, location, val, size);

This instruction is taken from here.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Michael Munta
  • 207
  • 2
  • 16
  • 5
    It's not a cast; `JImageGetResource` is a variable, and it's a function pointer. (Search for it on that page.) – molbdnilo Sep 22 '20 at 10:15
  • How is `JImageGetResource` defined? Without the definition you'll get plausible guesses that may or may not be right. – Pete Becker Sep 22 '20 at 13:12

3 Answers3

2

There's no cast involved. JImageGetResource is a pointer, by adding * in front of it, it will be dereferenced. Typically you will see this with iterators for example.

In your case it's a function pointer. So the resulting code would be the same as resultingFunction(jimage, location, val, size).

In line 1017: JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));

deW1
  • 5,562
  • 10
  • 38
  • 54
1

No, that is no cast, because the thing in the left bracket is no type. It is an usual function call.

The left bracket dereference the pointer JImageGetResource, and the obtained object is something that can be called with 4 arguments.

gerum
  • 974
  • 1
  • 8
  • 21
  • Without context, `JImageGetResource` is not necessarily a pointer. It can also be of a class type with overloaded unary operator `*`. And the result is not necessarily an object; it can also be a function. – eerorika Sep 22 '20 at 10:27
  • I partly second eerorika, from the code included in the question one cannot know what it is. Perhaps you are right, but you should at least refer to what is missing in the question that made you come to the conclusion – 463035818_is_not_an_ai Sep 22 '20 at 10:36
  • @eerorika Your right, it could be that it is no pointer, but it is initialized with NULL, so it is at least pointer like. – gerum Sep 23 '20 at 11:06
  • @eerorika An function pointer is an object too, isn't it? – gerum Sep 23 '20 at 11:09
  • @gerum Yes. Pointers are objects. – eerorika Sep 23 '20 at 11:11
1

Taking this declaration into account

static JImageGetResource_t             JImageGetResource      = NULL;

it means that the name JImageGetResource identifies a pointer of the type JImageGetResource_t that is initialized by NULL.

Also this record

 JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));

is self-documented and means JImageGetResource denotes a function pointer.

So this expression statement

(*JImageGetResource)(jimage, location, val, size);

represents a function call with four arguments.

This record could be written also like

JImageGetResource(jimage, location, val, size);

or to make a more obfuscating code (due to the implicit conversion of a function designator to pointer to the function) even like:)

(***JImageGetResource)(jimage, location, val, size);

Here is a demonstrative program.

#include <iostream>

void hello( const char *s )
{
    std::cout << "Hello " << s << '\n';
}

typedef void ( *hello_t )( const char * );

int main() 
{
    hello_t fp = NULL;
    
    fp = hello;
    
    fp( "Michael Munta" );
    ( *fp )( "Michael Munta" );
    ( ***fp )( "Michael Munta" );
    
    return 0;
}

Its output is

Hello Michael Munta
Hello Michael Munta
Hello Michael Munta
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Out of curiosity, I googled a bit to find out how `JImageGetResource_t` is actually defined. (Google is not that clever that I thought.) I got **1** reasonable hit with something looking like a git commit uncovering: `typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location, char* buffer, jlong size);` (from http://hg.openjdk.java.net/harfbuzz/jdk9/hotspot/rev/0316b41ccb2f). – Scheff's Cat Sep 22 '20 at 11:10