need help

This is a discussion on need help within the C Programming forums, part of the General Programming Boards category; ok i did this prog, but i get like 20 errors like this : Error: noname00.cpp(67,51):Cannot convert 'void (*)(char *,long)' ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    need help

    ok i did this prog, but i get like 20 errors like this :
    Error: noname00.cpp(67,51):Cannot convert 'void (*)(char *,long)' to 'void (*)()'
    Error: noname00.cpp(67,51):Type mismatch in parameter 'fill' in call to 'fill_array(void *,long,void (*)())'

    this is my prog

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    
    
    
    void fillchar(char *a, long len);
    void fillshort(short *a, long len);
    void fillong(long *a, long len);
    void fillfloat(float *a, long len);
    void filldouble(double *a, long len);
    
    
    
    void printchar(char *a, long s, long e);
    void printshort(short *a, long s, long e);
    void printlong(long *a, long s, long e);
    void printfloat(float *a, long s, long e);
    void printdouble(double *a, long s, long e);
    
    
    
    void fill_array(void *arr, long len, void (*fill)());
    void display_array(void *arr, long start, long end, void (*d)());
    void wait(long sec);
    
    unsigned init;
    
    main()
    {
    
    
    
       char c[1000];
       short s[100];
       long l[100];
       float f[100];
       double d[100];
       int number, i;
       long index[2];
    
       while (!kbhit())
         {
           srand(++init);
           if(init == 65535)
              init = 1;
           number = rand() % 5 + 1;
    
    
           do
              {
                for (i = 0; i < 2; i++)
                  index[i] = (number != 1) ? rand()%100 : rand()%1000;
              } while (index[0] >= index[1]);
    
           wait(3);
           printf("\033[2J\n");
    
    
    
           printf("\033[%d;%dm", 31+number, 42+number);
    
           switch(number)
             {
              case 1:    printf("C H A R :\n\n");
                         fill_array(c, 1000, fillchar);
                         display_array(c, index[0], index[1], printchar);
                         break;
             case 2:
                        printf("S H O R T :\n\n");
                        fill_array(s, 100, fillshort);
                        display_array(s, index[0], index[1], printshort);
                        break;
             case 3:
                        printf("L O N G :\n\n");
                        fill_array(l, 100, fillong);
                        display_array(l, index[0], index[1] ,printlong);
                        break;
             case 4:
                        printf("F L O A T :\n\n");
                        fill_array(f, 100, fillfloat);
                        display_array(d, index[0], index[1], printfloat);
                        break;
             case 5:
                        printf("D O U B L E :\n\n");
                        fill_array(d, 100, filldouble);
                        display_array(d, index[0], index[1], printdouble);
             }
         
    }
       printf("\033[0m");
    }
    
    
    
    void fill_array(void *arr, long len, void (*fill)(void*, long))
    {
       (*fill)(arr, len);
    }
    
    
    void fillchar(char *a, long len)
    {
       long i;
       srand(++init);
       for (i = 0; i < len; i++)
       {
       a[i] = (rand() % 96) + 32;
       }
    }
    
    
    void fillshort(short *a, long len)
    {
       long i;
       srand(++init);
       for (i = 0; i < len; i++)
       a[i] = rand();
    }
    
    
    void fillong(long *a, long len)
    {
       long i;
       srand(++init);
       for (i = 0; i < len; i++)
       a[i] = (long) rand() * rand();
    }
    
    
    void fillfloat(float *a, long len)
    
    {
       long i;
       srand(++init);
       for (i = 0; i < len; i++)
       a[i] = (float) rand() / 32767;
    }
    
    
    void filldouble(double *a, long len)
    
    {
       long i;
       srand(++init);
       for (i = 0; i < len; i++)
       a[i] = (double) rand() / 32767;
    }
    
    
    
    void display_array(void *arr, long start, long end, void (*f)(void*, long, long))
    {
       (*f)(arr,start, end);
    }
    
    
    void printchar(char *a, long s, long e)
    {
       long i;
       for (i = s;  i <= e; i++)
       printf("%c", a[i]);
    }
    
    
    void printshort(short *a, long s, long e)
    {
       long i;
       for (i = s;  i <= e; i++)
       {
       printf("%5hd  ", a[i]);
       if (((i+1)%10) == 0)
       printf("\n");
       }
    }
    
    
    void printlong(long *a, long s, long e)
    {
       long i;
       for (i = s;  i <= e; i++)
       {
       printf("%10ld  ", a[i]);
       if (((i+1)%6) == 0)
       printf("\n");
       }
    }
    
    
    void printfloat(float *a, long s, long e)
    {
       long i;
       for (i = s;  i <= e; i++)
       {
       printf("%f  ", a[i]);
       if (((i+1)%6) == 0)
       printf("\n");
       }
    }
    
    
    void printdouble(double *a, long s, long e)
    {
       long i;
       for (i = s;  i <= e; i++)
       printf("%.12f  ", a[i]);
    }
    
    
    void wait(long sec)
    {
       long start, current;
       time(&start);
       do
       {
       time(&current);
       } while ((current - start) < sec);
    }
    so what did i do wrong? i put the peice of prog in red where i get my errors
    Last edited by emperor; 03-04-2002 at 11:51 AM.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Error: noname00.cpp(67,51):Cannot convert 'void (*)(char >*,long)' to 'void (*)()'

    This is a pointer to a function. The pointer is of type void and requires a char*-parameter and a long-parameter as input. You didn't do that.

    You prototyped:
    void fill_array(void *arr, long len, void (*fill)());

    And use:
    fill_array(c, 1000, fillchar);

    Where fillchar is declared as:
    void fillchar(char *a, long len);

    That's why things go wrong. I think it isn't possible to do it the way you do. The function pointers should match. So if you declare

    void (*fill)(char*, long)

    then function where the pointer points to should also be of the same type.

    What you could do is something like

    void (*fill)(void *, void)

    and perform casting. Perhaps you could create a union which you pass.

    >Error: noname00.cpp(67,51):Type mismatch in parameter 'fill' in >call to 'fill_array(void *,long,void (*)())'

    See above.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21