title: How to return different types from a single function
body: <p>I have the following c code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void *func(int a) { 
      if (a==3) {
          int a_int = 5;
          int *ptr_int = &amp;a_int;
          return (void *)ptr_int;
      } 
      else if (a==4) {
          char a_char = 'b';
          char *ptr_char = &amp;a_char;
          return (void *)ptr_char;
      }
      else {
          fprintf(stderr, "return value is NULL");
          return NULL;
      }
  }
  
  int main (int argc, char *argv[]) {
      int *ptr_int = (int *)func(3);
      char *ptr_char = (char *)func(4);
      fprintf(stdout, "int value = %d\n", *ptr_int);
      fprintf(stdout, "char value = %c\n", *ptr_char);
      return 0; 
  }
  </code></pre>
  
  <p><br>
  But when I use gcc to test this code, I have the following results :
  <br></p>
  
  <blockquote>
  <pre><code>int value = 98
  char value = �
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = 
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = 
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = 
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = 
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = g
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = 
  root@coupure:/home/bohao/Desktop/test1375# gcc test.c -o test
  root@coupure:/home/bohao/Desktop/test1375# ./test 
  int value = 98
  char value = !
  </code></pre>
  </blockquote>
  
  <p>Why do I have the <strong>98</strong> for ptr_int and the <strong>random value</strong> of ptr_char ? <br>
  What I want is to have <strong>a general function</strong> who can return different types of values instead of using two functions. Is that possible ? If so, how to do it ? </p>