Generic function

This is a discussion on Generic function within the C++ Programming forums, part of the General Programming Boards category; Moved to C++ programming forum....

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Moved to C++ programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> i am trying to write one Generic function
    Why are you trying to do this? At this point, we understand "what" you are trying to solve, but not "why".
    Why are you locked in on implementing "one Generic function"? It's very likely that there are better ways to solve the "root" problem.

    gg

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Thank you,
    for Reply , but i want to implement this in pure c only,
    uding void pointers any help in this direction.

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> i want to implement this in pure c only
    Can't be done in standard C.

    gg

  5. #20
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,176
    O_o

    Are you trying to write your software so that it's easy to exploit?

    Soma

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Hello experts,

    i dont know why this post is trasfered to c++ forum,
    any how i am trying to do some thing like this,

    is it possible to use the call by reference for more than 3 levels for ex:



    Code:
    int main()
    {
    int a=0,b=10;
    fun_call(&a,&b);
    printf("\n a=%d b=%d",a,b);//i want the output as a=10 b=10
    
    return 0;
    }
    
    void fun_call( int *a,int *b)
    
    {
        int m=a;
        int n=b;
           fun_transver(&m,&n);
    }
    
    void fun_transver(int *m,int *n)
    {
    m=n;
    }
    in this way at last in my main method i should get the result both as 10 , even though this is a simple example can be done in two steps only but for some part of my projec i require this type of flow so how to achieve this please help me. how to pass by reference for more than one time.
    Last edited by vin_pll; 02-03-2009 at 04:32 AM.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have explained perfectly WHAT you want to do, but not why.

    The only way you can pass arguments to a function and not know until you get to the function, what those arguments represent, would be by passing another pass a "type" information with the argument.

    C is not a particularly good language for this purpose.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The example code seems to be a completely different problem. Sure you can pass the pointers on as many times you like:

    Code:
    void fun_call( int *a,int *b)
    {
        /*
        int* m=a;
        int* n=b;
        fun_transver(m, n);
        */
        fun_transver(a, b);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Dear experts,

    as i said i require this piece mainly for some other part of my project,
    may be my syntax may be wrong,

    please tell me how to pass by reference for more than one level so at last i should get the value in main, as required,

    Code:
    int main()
    {
    int a=0,b=20;
    call_fun1(....)
    print("\n a=%d b=%d",a,b);//at last i want a=10 b=10
    }
    
    call_fun1(int *, int *)
    {
    call_fun2(...)
    }
    
    
    call_fun2(int *,int *)
    {
    a=b;
    }
    i want at last a and b as 10 what is the correct way or syntax i am asking that
    any help in this regard is favourable.

  10. #25
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Code:
    #include <stdio.h>
    
    void call_fun1(int *pa, int *pb);
    void call_fun2(int *pa, int *pb);
    
    int main()
    {
        int a=0, b=20;
        call_fun1(&a, &b);
        printf("a=%d, b=%d\n", a, b);
        return 0;
    }
    
    void call_fun1(int *pa, int *pb)
    {
        call_fun2(pa, pb);
    }
    
    void call_fun2(int *pa, int *pb)
    {
        *pa = *pb = 10;
    }
    gg

  11. #26
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Thank you codeplug,

    really u gave the correct syntax even though the example was simple,
    but it solved my big problem i was able to modify lot of things thanks a lot again.

    again what to do if one of the variable is a pointer this actually i want to ask if
    void *a=0, b=20;
    how to achieve the same result, how to convert from void * to int and vice versa,
    what is the exact syntax in this case.
    Last edited by vin_pll; 02-03-2009 at 10:23 PM.

  12. #27
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Code:
    #include <stdio.h>
    
    void call_fun1(void *pa, void *pb);
    
    int main()
    {
        int a=0, b=20;
        call_fun1(&a, &b);
        printf("a=%d, b=%d\n", a, b);
        return 0;
    }
    
    void call_fun1(void *pa, void *pb)
    {
        *((int*)pa) = 10;
        *((int*)pb) = 10;
    }
    http://www.amazon.com/Pointers-C-Ken.../dp/0673999866

    gg

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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