logo
down
shadow

Function signature indicates return type void*, but returns pointer of a different type


Function signature indicates return type void*, but returns pointer of a different type

By : user2956881
Date : November 22 2020, 03:03 PM
I hope this helps .
Why is return freeBlock,which returns type BlockInfo* a valid statement.
code :
char a = 42;
void *p = &a;
int *q = p;

*q;  // undefined behavior


Share : facebook icon twitter icon
why is a pointer function always have void return type?

why is a pointer function always have void return type?


By : Jonathan Hardwick
Date : March 29 2020, 07:55 AM
this will help A function pointer can have pretty much any return type. Consider this example:
code :
#include "stdafx.h"
#include <iostream>

using namespace std;

// this defines a type called MathOp that is a function pointer returning
// an int, that takes 2 int arguments
typedef int (*MathOp)(int, int);

enum MathOpType
{
    Add = 1,
    Subtract = 2
};

// some generic math operation to add two numbers
int AddOperation(int a, int b)
{
    return a+b;
}

// some generic math operation to subtract two numbers
int SubtractOperation(int a, int b)
{
    return a-b;
}

// function to return a math operation function pointer based on some argument
MathOp GetAMathOp(MathOpType opType)
{
    if (opType == MathOpType::Add)
        return &AddOperation;

    if (opType == MathOpType::Subtract)
        return &SubtractOperation;

    return NULL;
}


int _tmain(int argc, _TCHAR* argv[])
{
    // declare a variable with the type MathOp, which is a function pointer to
    // a function taking two int arguments, and returning an int.
    MathOp op = &AddOperation;
    std::cout << op(2, 3) << std::endl;

    // switch the operation we want to perform by calling our one op variable
    op = &SubtractOperation;
    std::cout << op(2, 3) << std::endl;

    // just an example of using a function that returns a function pointer
    std::cout << GetAMathOp(MathOpType::Subtract)(5, 1) << std::endl;

    std::getchar();

    return 0;
}
Why can't methods that have a return type match a delegate of the same signature that returns void?

Why can't methods that have a return type match a delegate of the same signature that returns void?


By : sgn
Date : March 29 2020, 07:55 AM
should help you out The C# specification (section 15.2 Delegate Compatibility) had this to say.
code :
public Action Ignore<T>(Func<T> call)
{
    return () => call();
}
how to use void pointer as function return type In C

how to use void pointer as function return type In C


By : user7062686
Date : March 29 2020, 07:55 AM
Any of those help So I am planing to Write a function to return a random array element. The function accept two parameters—an array of void pointers and the array length. It should return a void pointer. The idea is to take the given array, which comes in the form of an array of void pointer, the function will return an random element of the the array. The question I have is, what do I need to do to return a pointer, what do I need to do to the "result" so I can return it like that? After wards what do I need to do to access it again? , You can write a perfectly fine generic function this way, but:
code :
    result = array[rand()%(length-1)];
    result = array + rand()%(length-1);
void *randomNum(void * array, size_t size, size_t length)
    result = (char*)array + (rand()%(length-1)) * size;
                   ^                            ^
    printf("%d\n", *(int*)randomNum(array,sizeof(*array),9));
Pointer to a function and its return type (void)

Pointer to a function and its return type (void)


By : DanjoMaster
Date : March 29 2020, 07:55 AM
Hope that helps Is such declaration void *(*function) () is valid ? If it is valid then *function will return any address to called function . At that address, what value is returning? Is value save at that address is 0. If it is zero,what is the difference between return 0 and return nothing in function having return type void. , The declaration is read as follows:
code :
        function        -- function is a 
       *function        -- pointer to 
      (*function) ()    -- function taking unspecified parameters
     *(*function) ()    -- returning pointer to
void *(*function) ();   -- void
void *foo( void )    { ... }
void *bar( void )    { ... }
void *bletch( void ) { ... }
if ( condition1 )
  function = foo;
else if ( condition2 )
  function = bar;
else
  function = bletch;

void *ptr = function(); // or (*function)();
Casting lambda with non-void return type to function pointer

Casting lambda with non-void return type to function pointer


By : Leonel Alejandro
Date : March 29 2020, 07:55 AM
it fixes the issue The problem is that you are using C-style explicit casts. These are notoriously dangerous.
Here in this case the problem is that fun3 (in contrast to fun2) already decays to a function pointer of type int(*)().
code :
reinterpret_cast<voidFunctionType>(fun3)
auto casted_fun2 = reinterpret_cast<voidFunctionType>(+fun2);
Related Posts Related Posts :
  • C: Unable to store lines from a file into an array
  • Should I be using GTK threads/Good tutorials on GTK threading?
  • Program works but outputs trailing garbage values
  • Letting 2 pointer pointing to same address
  • Different ways to print the two-dimensional array's contents
  • C Programming : Confusion between operator precedence
  • C code inside a loop not being executed
  • C - Weird symbols
  • C - Get pointer adress to string
  • how to start a function using a Struct?
  • Trying to tweak sscanf() to ignore \n and \t
  • How to find the inverse of a Rectangular Matrix in C using GSL
  • sizeof() showing different output
  • How to select/read/copy values after specific character in a string
  • Jump from bootloader generates exception
  • Array dropping values, picks up garbage
  • Swig: Syntax error in input(3)
  • multiple definition and making sure function is correctly written
  • MD4 openssl core dumped
  • Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing?
  • Why am i getting problem3.c:20:23: error: expected expression before ‘int’?
  • Right Justified Zero filled String in C
  • C Function with parameter without type indicator still works?
  • How to transmit data from an interrupt handler to an user application?
  • Why do I get the error "bash: ./a.out: Permission denied" when I execute a C program in Linux mint 15
  • syntax of sigchld and its declaration
  • error using g_idle_add() in C++, same thing works in C
  • why if else or nested if else are called single statement in C
  • How do I interpret this printf in C
  • load the functions of a shell script without executing it
  • Is FilterSendNetBufferLists handler a must for an NDIS filter to use NdisFSendNetBufferLists?
  • How to write to flash memory using inline assembly?
  • More Return Statements vs. More Indentation
  • How to show an image on a PictureBox from resource?
  • Having malloced some memory,I could't calculator the proper size of the memories I malloced.I don't know why
  • What is the main difference between integer pointer and character pointer?
  • Why are some functions declared extern and header file not included in source in Git source code?
  • what is the use of fflush(stdin) in c programming
  • Is it safe to return file File descriptor locally allocated from another function In C
  • Changing undefined values of an array
  • What does an empty parameter list mean?
  • using strtol on a string literal causing segmentation fault
  • Same structure objects memory overlap?
  • C-Linux-Any way to pass command "history" to Linux shell?
  • Using #define in defining string size C
  • How to use thread pool and message queues in Multithreaded Matrix Multiplication?
  • Can't find how to select path to run a C program
  • Automatic variable in C not initialized but given fixed value within loop
  • main() function defined without return type gives warning
  • Output of following code with integer, float, char variable
  • why buffer memory allocation error in opencl
  • Why am I getting this error during run-time?
  • Strange behaviour of the pow function
  • task in increment , decrement , printf() , why these are evaluated in this manner in C
  • 28 extra bytes in bss
  • Waiting for multiple events without polling
  • Why are my variables reporting as "undeclared identifier" when compiling?
  • Correct AddNode function but somehing happens when I printf
  • When I traverse in the splay tree, then now which one is root?
  • Data type conversion in Postfix evaluation
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org