Function pointers in CPP and C #

related codes

-sharpinclude <stdio.h>
-sharpinclude <stdlib.h>
-sharpinclude <pthread.h>

void *print_message_function( void *ptr );

main()
{
     pthread_t thread1, thread2;
     const char *message1 = "Thread 1";
     const char *message2 = "Thread 2";
     int  iret1, iret2;

     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
     if(iret1)
     {
         fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
         exit(EXIT_FAILURE);
     }

     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
     if(iret2)
     {
         fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
         exit(EXIT_FAILURE);
     }

     printf("pthread_create() for thread 1 returns: %d\n",iret1);
     printf("pthread_create() for thread 2 returns: %d\n",iret2);

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     exit(EXIT_SUCCESS);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}

confused place

in the function pthread_create (pthread_t * thread, const pthread_attr_t * attr,void * (* start_routine) (void *), void * arg);
), where void * (* start_routine) (void *) is the parameter "function pointer"
, and according to the function void * print_message_function (void * ptr);) From a prototype point of view, this is a function that returns any pointer type, not a function pointer, and there is no return value in the implementation of this function. But it can be compiled using gcc without any exceptions. Does anyone know why? Or give a reference link?

the use of functions like void * print_message_function (void * ptr);) that I know is


 -sharpinclude<stdio.h>

 void *test_f(int *n);
 
 int main(void) 
{
         int test_num = 32;
         int *test_p = &test_num;
         int *ret_v = test_f(test_p);
         printf("%d \n",*ret_v);
         return 0;
 }

void *test_f(int *n) 
{
         *n = *n + 1;
         return n;
}
CPP c
Apr.03,2021

Let's start with a question: there is a problem with the return statement in your last function void * test_f (int * n) . It should be return & n;

.

then answer your questions--

first of all, in the implementation of your void * print_message_function (void * ptr) function, there is no explicit use of calling return statement. As far as I know about some compilers, the compiler will automatically set a return value and return it, usually 0 . When compiling, a warning error is usually reported. If you do not see the relevant error, the compiler may have disabled the relevant error.

then the function pointer. In C #, arrays and functions have an interesting feature in common, as shown in the following code:

http://man7.org/linux/man-pag...</a>

<hr>

gcc <strong>-Wreturn-type</strong> " void return " <br> <strong>-Wall</strong>

gcc -Wall a.c

if you compile with gPP, you don't have to specify -Wreturn-type explicitly, because it dictates that return be written (return values for non-void).

Menu