mardi 27 août 2019

How can we change the type of void pointer to int type or float type?

I want to convert a void pointer to integer pointer type or float pointer type. My code takes input like what's the data type and on the basis of that, I want to create an array of that kind of data type. So for this, I have created a void pointer and I am trying to cast it to respective data types. And now I have overloaded three functions of the same name with different data types. Or how can I achieve this with void pointer??

   while (1) {
    printf("(1)Integer\n(2)Float\n(3)Char\n");
    ReadMenu(inp);

    if (StrCmp(inp, "1")) {

        arr_int = (int*)arr;
        arr = arr_int ;
        what = 'i' ;
    } else if (StrCmp(inp, "2")) {

        arr = (float*)arr;
        arr = arr_float ;
        what = 'f' ;
    } else if (StrCmp(inp, "3")) {

        arr_char = (char*)arr;
        arr = arr_char ;
        what = 'c' ;
    } else {
        PrintErrInp();
        continue;
    }
while (1) {
    printf("(1)Insert\n(2)Top\n(3)Pop(4)IsEmpty\n(5)Exit\n");
    ReadMenu(inp);
    if (StrCmp(inp, "1")) {

        cout << "Enter an Element: ";
        bool isit = '\0';
        ReadString(str);
        if (what == 'i') {

            isit = IsInt(str) ;
        } else if (what == 'f') {

            isit = IsFloat(str) ; 
        } else {

            isit = IsChar(str) ;
        }
        if (isit) {

            InsertStack(arr, inp, size) ;
        } else {
            PrintErrInp() ;
            continue ;
        }
    }

Q2_1.cpp(230): error C2665: 'InsertStack': none of the 3 overloads could 
convert all the argument types
Q2_1.cpp(131): note: could be 'void InsertStack(char [],char,size_t &)'
Q2_1.cpp(84): note: or       'void InsertStack(double [],double,size_t &)'
Q2_1.cpp(36): note: or       'void InsertStack(int [],int,size_t &)'
Q2_1.cpp(230): note: while trying to match the argument list '(void *, 
char *, size_t)'

Aucun commentaire:

Enregistrer un commentaire