Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to pass an argument through ... of one function to ... of another

RIGHT_THENRIGHT_THEN Member Posts: 154
GentleMen,
how to pass an argument from one function`s ...
to the ... of another;
Example:-
[code]
int Myprintf(const char *Arguments, ...)
{ From here _____________/ |
To here _____________ |
|
printf(const char *Arguments, ...);
}
[/code]
1)Solution:- i thought
[code]
int Myprintf(const char *Arguments,...)
{
int ReturnValue;
va_list ListArg;
va_start(ListArg,Arguments);
ReturnValue = printf(Arguments,ListArg);
va_end (ListArg);
return ReturnValue;
}
/*Failed*/
[/code]
2)Solution:- i thought
[code]
int Myprintf(const char *Arguments,...)
{
int ReturnValue, gn ;
va_list ListArg ;

va_start(ListArg,Arguments);
char *Args = (char *)malloc(sizeof(ListArg));

for(gn=0;gn<=(sizeof(ListArg));gn++)
{
Args[gn]=va_arg(ListArg,int);
//printf("%d ",Args[gn]);
}
ReturnValue = printf(Arguments,Args);
va_end (ListArg);
return ReturnValue;
}
/*Failed*/
[/code]
3)Solution:- i though
[code]
int Myprintf(const char *Arguments,...)
{
int ReturnValue;
va_list ListArg;
ReturnValue = printf(Arguments,ListArg);
va_end (ListArg);
return ReturnValue;
}
/*Failed*/
[/code]
4)Solution;- i thought
[code]
int Myprintf(const char *Arguments,...)
{
ReturnValue = printf(Arguments,...);
/*ANd*/
ReturnValue = printf(Arguments,"...");
/*ANd*/
ReturnValue = printf(Arguments,*"...");
/*And*/
ReturnValue = printf(Arguments,*(...));

return ReturnValue;
}
/*Miserablebly Failed Curupted
The rules and Sense of C This TIme*/
[/code]
_vsntprintf i Know this one to get the
results of this kind
but that is not what is wanted QUESTION is
How to Pass ... of one Function just like that
to ... of another Function. ;-)

HAVE A NICE TIME CURRUPTING YOUR PEACE OF MIND
WITH THIS :-)




Comments

  • DonotaloDonotalo Member Posts: 715
    How to use variable length parameter list [blue]...[/blue]?
  • RIGHT_THENRIGHT_THEN Member Posts: 154
    Your Question:- How to use variable length parameter list [blue]... [/blue]?
    Well Sir,
    See if this is what you wanted to know or are you asking me To
    know it
    [code]
    #include
    #include
    #include
    #include
    #include
    #include
    int VariableParameters(char *,...);
    void main()
    {
    clrscr();

    int Num1 = 100;
    char *String ="Is Hundered" ;
    char *String1 ="Isnt It!!" ;
    VariableParameters("%d %s %s",Num1,String,String1);

    getch();

    }
    [/code]
    [code]
    int VariableParameters(char *Parameter,...)
    {
    char *Buffer ;

    /*For Putting the ... Arguments in
    va_list macro Kind Of ListArgs;*

    va_list ListArgs ;

    /*For Pointing To the First Element
    of "ListArgs" While "Parameter" is
    For telling where to type or where
    is that first element by traversing
    through it THIS IS MUST IN ORDER TO
    GET ANYTHING OUT Of IT WHEN WE PASS
    IT TO A FUNCTION OR USE IT OurSelves
    FOr Traversing in order to get the output */

    va_start (ListArgs, Parameter ) ;
    Buffer = (char *) malloc (1000) ; /*you know why this is*/

    /* this Function then takes
    the "ListArgs" WHich is
    POinting TO the First
    ARgument Of ... and Also Takes
    "Parameter"and stores the
    resultant in the buffer */

    vsprintf (Buffer, Parameter, ListArgs);

    printf ("%s", Buffer) ; /*to see What happened*/
    va_end (ListArgs ) ; /*it is proper to end
    ListArgs just as we
    free the buffer althou
    two are different
    things*/
    free (Buffer ) ; /*you know WHy this is!!*/

    return 0; /* Now the most difficult Part or thing to programm.
    There is so much of logic in return. First Key to Press
    is "r" or "enter" which one?
    if you are not in proper line Thinking.....*/
    }
    [/code]
  • DonotaloDonotalo Member Posts: 715
    : Your Question:- How to use variable length parameter list [blue]... [/blue]?
    : Well Sir,
    : See if this is what you wanted to know or are you asking me To
    : know it
    : [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : int VariableParameters(char *,...);
    : void main()
    : {
    : clrscr();
    :
    : int Num1 = 100;
    : char *String ="Is Hundered" ;
    : char *String1 ="Isnt It!!" ;
    : VariableParameters("%d %s %s",Num1,String,String1);
    :
    : getch();
    :
    : }
    : [/code]
    : [code]
    : int VariableParameters(char *Parameter,...)
    : {
    : char *Buffer ;
    :
    : /*For Putting the ... Arguments in
    : va_list macro Kind Of ListArgs;*
    :
    : va_list ListArgs ;
    :
    : /*For Pointing To the First Element
    : of "ListArgs" While "Parameter" is
    : For telling where to type or where
    : is that first element by traversing
    : through it THIS IS MUST IN ORDER TO
    : GET ANYTHING OUT Of IT WHEN WE PASS
    : IT TO A FUNCTION OR USE IT OurSelves
    : FOr Traversing in order to get the output */
    :
    : va_start (ListArgs, Parameter ) ;
    : Buffer = (char *) malloc (1000) ; /*you know why this is*/
    :
    : /* this Function then takes
    : the "ListArgs" WHich is
    : POinting TO the First
    : ARgument Of ... and Also Takes
    : "Parameter"and stores the
    : resultant in the buffer */
    :
    : vsprintf (Buffer, Parameter, ListArgs);
    :
    : printf ("%s", Buffer) ; /*to see What happened*/
    : va_end (ListArgs ) ; /*it is proper to end
    : ListArgs just as we
    : free the buffer althou
    : two are different
    : things*/
    : free (Buffer ) ; /*you know WHy this is!!*/
    :
    : return 0; /* Now the most difficult Part or thing to programm.
    : There is so much of logic in return. First Key to Press
    : is "r" or "enter" which one?
    : if you are not in proper line Thinking.....*/
    : }
    : [/code]

    thank u RIGHT_THEN. i am trying to understand the code! i am a learner. so i cant check whether u know that thing or not. i didnt know, i wanna learn.
  • MT2002MT2002 Member Posts: 1,444
    : GentleMen,
    : how to pass an argument from one function`s ...
    : to the ... of another;
    : Example:-
    : [code]
    : int Myprintf(const char *Arguments, ...)
    : { From here _____________/ |
    : To here _____________ |
    : |
    : printf(const char *Arguments, ...);
    : }
    : [/code]
    [blue]
    Why would you want to do something like that? I *never*
    seen var-leanth args used that way.

    You *might* be able to reverse-engineer the va_*macros.

    Why not just do this instead?:[/blue][code]
    void MyPrintf (const char* msg, ...)
    {
    char szBuf[1024];
    va_list args;

    va_start(args,msg);
    vsprintf(szBuf,msg,args);
    va_end (args);

    printf ("%s",szBuf);
    }[/code][blue]
    I wouldnt recommend using printf() anyway.[/blue]
  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : : GentleMen,
    : : how to pass an argument from one function`s ...
    : : to the ... of another;
    : : Example:-
    : : [code]
    : : int Myprintf(const char *Arguments, ...)
    : : { From here _____________/ |
    : : To here _____________ |
    : : |
    : : printf(const char *Arguments, ...);
    : : }
    : : [/code]
    : [blue]
    : Why would you want to do something like that? I *never*
    : seen var-leanth args used that way.
    :
    : You *might* be able to reverse-engineer the va_*macros.
    :
    : Why not just do this instead?:[/blue][code]
    : void MyPrintf (const char* msg, ...)
    : {
    : char szBuf[1024];
    : va_list args;
    :
    : va_start(args,msg);
    : vsprintf(szBuf,msg,args);
    : va_end (args);
    :
    : printf ("%s",szBuf);
    : }[/code][blue]
    : I wouldnt recommend using printf() anyway.[/blue]
    :
    Sir Mr. MT2002

    Thankyou For Your Time

    In The Above Example Printf is used simply to
    Demostrate what i want .Out Of Curiosity i want To Know it
    if at all i was faced with a similar situation in someother
    respect where i wanted to pass A variable length Argument ... Of
    One Fucntion to the Variable length Parameter ... of another.

    Why I did It At all Because i am Learning C.
    and I am TRying TO Learn it all
    So As you Know One Cannot Memorise all the functions
    Available in VC++ for WIndows.
    so what i am DOing is Making Structures and putting
    aLL the Functions in That Stucture Say All the Functions
    Belonging TO the Handling Of files Are put Inside the
    Structure FileFuncs so in order to use it
    i will simply have to make an object of FileFuncs and when
    i will Put "." or "->" in front of that object all the
    available functions for the purpose of file handling will
    be visible to me in vc++ editor instead of going back and
    searching in help.So it Will make things Easy for me.That is Why!

    HINT:-(" GO Directly TO the Bottom Of this Reply TO SEE WhatI mean")


    So All the Functions Belonging TO the Output IF Were TO Be
    Put In a Structure CAlled OutPutFuncs;Like this
    [code]
    struct OutPutFuncs
    {
    int printf (const char *,...) ;
    FILE* fopen (char *,const char *) ;
    fclose (FILE *) ;
    };

    Would Not Let me use the Printf in a way it is
    designed too Because the compiler will understand that this is
    a local function within the Structure OutPutFuncs

    SO what Is the Solution It Is THis
    struct OutPutFuncs
    {
    int Myprintf (const char *,...) ; //defined
    FILE* Myfopen (char *,const char *) ; //them
    Myfclose (FILE *) ; //here
    };

    /*declaring them here */
    int OutPutFuncs::Myprintf(const char *Arguments,...)
    {
    int ReturnValue;
    ReturnValue = printf(Arguments,...);/*but Putting ... here is wrong*/
    return ReturnValue; /*what to write instead of ...
    /*i have already written in my
    /*initial Post
    }

    SO this is how every function of the operating system will
    still work the same way by building a Function over a function.
    There are other ways to do it too.BUT HOW TO Pass ... OF One
    FUnction TO another.

    But fopen Works FIne Because No ... is there in it
    Like THis

    FILE * Output::Myfopen(char *FileNamePath,const char *Mode)
    {
    FILE *FilePointer ;
    FilePointer = fopen(FileNamePath ,Mode) ;
    return FilePointer ;
    /*this would Work just as we were using fopen*/
    }

    Something like this Althou to get the correct output Pointer
    has to be implemented more correctly But you
    Get the Idea what i am saying


    THis Is WHat I mean When I say I will See EveryFUnc in the Editor
    When I will Press "."

    struct OutPutFuncs OutPutFuncs_obj1 ;

    OutPutFuncs_Obj1.______________
    |Myprintf |
    |MyFopen |
    |MyFclose |
    |etc.... |
    |RestOfTheFuncs|
    |WhichMightHave|
    |BeenAddedTO |
    |Struct |
    |______________|

    [/code]

    Have i Made My self Clear Or Messed It Up More
    And YOU Sir Used A Word "REVERSE ENGENNERING" do you know
    anything about it i Might have alot to learn from you
    then.

  • MT2002MT2002 Member Posts: 1,444
    : : : GentleMen,
    : : : how to pass an argument from one function`s ...
    : : : to the ... of another;
    : : : Example:-
    : : : [code]
    : : : int Myprintf(const char *Arguments, ...)
    : : : { From here _____________/ |
    : : : To here _____________ |
    : : : |
    : : : printf(const char *Arguments, ...);
    : : : }
    : : : [/code]
    : : [blue]
    : : Why would you want to do something like that? I *never*
    : : seen var-leanth args used that way.
    : :
    : : You *might* be able to reverse-engineer the va_*macros.
    : :
    : : Why not just do this instead?:[/blue][code]
    : : void MyPrintf (const char* msg, ...)
    : : {
    : : char szBuf[1024];
    : : va_list args;
    : :
    : : va_start(args,msg);
    : : vsprintf(szBuf,msg,args);
    : : va_end (args);
    : :
    : : printf ("%s",szBuf);
    : : }[/code][blue]
    : : I wouldnt recommend using printf() anyway.[/blue]
    : :
    : Sir Mr. MT2002
    :
    : Thankyou For Your Time
    :
    : In The Above Example Printf is used simply to
    : Demostrate what i want .Out Of Curiosity i want To Know it
    : if at all i was faced with a similar situation in someother
    : respect where i wanted to pass A variable length Argument ... Of
    : One Fucntion to the Variable length Parameter ... of another.
    :
    : Why I did It At all Because i am Learning C.
    : and I am TRying TO Learn it all
    : So As you Know One Cannot Memorise all the functions
    : Available in VC++ for WIndows.
    [blue]
    Thats true--but most of the functions can be learned through
    exp. See below.
    [/blue]

    : so what i am DOing is Making Structures and putting
    : aLL the Functions in That Stucture Say All the Functions
    : Belonging TO the Handling Of files Are put Inside the
    : Structure FileFuncs so in order to use it
    : i will simply have to make an object of FileFuncs and when
    : i will Put "." or "->" in front of that object all the
    : available functions for the purpose of file handling will
    : be visible to me in vc++ editor instead of going back and
    : searching in help.So it Will make things Easy for me.That is Why!

    [blue]
    While it is a good idea to group sections of code that
    way--why are you rewriting printf(),fopen(),etc.? Writing
    a file manager is one thing--but [italic]printf()[/italic]?
    [/blue]

    : HINT:-(" GO Directly TO the Bottom Of this Reply TO SEE WhatI mean")
    :
    :
    : So All the Functions Belonging TO the Output IF Were TO Be
    : Put In a Structure CAlled OutPutFuncs;Like this
    : [code]
    : struct OutPutFuncs
    : {
    : int printf (const char *,...) ;
    : FILE* fopen (char *,const char *) ;
    : fclose (FILE *) ;
    : };
    [red]
    // Ouch! I recommend to redesign this. Files should be
    // seperate from string output. Also, defining
    // OutPutFuncs::printf() would be a useless wrapper that simply
    // adds extra uneccessary overhead. That was my point above.
    [/red]

    : Would Not Let me use the Printf in a way it is
    : designed too Because the compiler will understand that this is
    : a local function within the Structure OutPutFuncs
    :
    : SO what Is the Solution It Is THis
    : struct OutPutFuncs
    : {
    : int Myprintf (const char *,...) ; //defined
    : FILE* Myfopen (char *,const char *) ; //them
    : Myfclose (FILE *) ; //here
    : };
    :
    : /*declaring them here */
    : int OutPutFuncs::Myprintf(const char *Arguments,...)
    : {
    : int ReturnValue;
    : ReturnValue = printf(Arguments,...);/*but Putting ... here is wrong*/
    : return ReturnValue; /*what to write instead of ...
    : /*i have already written in my
    : /*initial Post
    : }
    :
    : SO this is how every function of the operating system will
    : still work the same way by building a Function over a function.
    : There are other ways to do it too.BUT HOW TO Pass ... OF One
    : FUnction TO another.
    :
    : But fopen Works FIne Because No ... is there in it
    : Like THis
    :
    : FILE * Output::Myfopen(char *FileNamePath,const char *Mode)
    : {
    : FILE *FilePointer ;
    : FilePointer = fopen(FileNamePath ,Mode) ;
    : return FilePointer ;
    : /*this would Work just as we were using fopen*/
    : }
    :
    : Something like this Althou to get the correct output Pointer
    : has to be implemented more correctly But you
    : Get the Idea what i am saying
    :
    :
    : THis Is WHat I mean When I say I will See EveryFUnc in the Editor
    : When I will Press "."
    :
    : struct OutPutFuncs OutPutFuncs_obj1 ;
    :
    : OutPutFuncs_Obj1.______________
    : |Myprintf |
    : |MyFopen |
    : |MyFclose |
    : |etc.... |
    : |RestOfTheFuncs|
    : |WhichMightHave|
    : |BeenAddedTO |
    : |Struct |
    : |______________|
    :
    : [/code]
    :
    : Have i Made My self Clear Or Messed It Up More
    : And YOU Sir Used A Word "REVERSE ENGENNERING" do you know
    : anything about it i Might have alot to learn from you
    : then.
    :
    [blue]
    I know what you are trying to do..But I recommend to plan
    this a little further into [b]objects[/b]. You should take
    a look at using OOP in C.

    "Reverse Engineering" is basically reversing how an
    alogrithm works to better understand it. In your case,
    you'd need to do the oppisite of the va_* macros. I dont
    think there is a way without brute-force hacking.[/blue]
  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : :[code]
    THis Is WHat I mean When I say I will See EveryFUnc in the Editor
    : : When I will Press "."
    : :
    : : struct OutPutFuncs OutPutFuncs_obj1 ;
    : :
    : : OutPutFuncs_Obj1.______________
    : : |Myprintf |
    : : |MyFopen |
    : : |MyFclose |
    : : |etc.... |
    : : |RestOfTheFuncs|
    : : |WhichMightHave|
    : : |BeenAddedTO |
    : : |Struct |
    : : |______________|
    : :
    : : [/code]
    : :
    :
    : "Reverse Engineering" is basically reversing how an
    : alogrithm works to better understand it. In your case,
    : you'd need to do the oppisite of the va_* macros. I dont
    : think there is a way without brute-force hacking.[/blue]
    :
    SIR
    THanks FOr Replying Again
    well Again i am Successful in getting through
    some points through you and yet missed on some.

    all your Suggestions are Appreciated
    I am not keen on writing printf either
    I am Only using it to illustrate the fact
    that How Can One Pass ... Of One Function
    To the ... Of another.
    Just for that Purpose And I dont Know How Yet.

    BRUTE-FORCE Hacking and REversal Of algorithm Are
    Very heavy words for a toddler like me in C.
    maybe after lots of practise in time to come
    they might become a second nature but seem
    Ghostly right now.

    BUt THanks ALL the way to LONDON for your
    REplies.Hope to trouble you again in future
    with bigger problems !!!














  • MT2002MT2002 Member Posts: 1,444
    [b][red]This message was edited by MT2002 at 2005-9-2 17:34:27[/red][/b][hr]
    : : :[code]
    : THis Is WHat I mean When I say I will See EveryFUnc in the Editor
    : : : When I will Press "."
    : : :
    : : : struct OutPutFuncs OutPutFuncs_obj1 ;
    : : :
    : : : OutPutFuncs_Obj1.______________
    : : : |Myprintf |
    : : : |MyFopen |
    : : : |MyFclose |
    : : : |etc.... |
    : : : |RestOfTheFuncs|
    : : : |WhichMightHave|
    : : : |BeenAddedTO |
    : : : |Struct |
    : : : |______________|
    : : :
    : : : [/code]
    : : :
    : :
    : : "Reverse Engineering" is basically reversing how an
    : : alogrithm works to better understand it. In your case,
    : : you'd need to do the oppisite of the va_* macros. I dont
    : : think there is a way without brute-force hacking.[/blue]
    : :
    : SIR
    : THanks FOr Replying Again
    : well Again i am Successful in getting through
    : some points through you and yet missed on some.
    :
    : all your Suggestions are Appreciated
    : I am not keen on writing printf either
    : I am Only using it to illustrate the fact
    : that How Can One Pass ... Of One Function
    : To the ... Of another.
    : Just for that Purpose And I dont Know How Yet.
    :
    : BRUTE-FORCE Hacking and REversal Of algorithm Are
    : Very heavy words for a toddler like me in C.
    : maybe after lots of practise in time to come
    : they might become a second nature but seem
    : Ghostly right now.
    :
    : BUt THanks ALL the way to LONDON for your
    : REplies.Hope to trouble you again in future
    : with bigger problems !!!
    [size=3]
    [blue]
    What I ment by "Brute-Force Hacking" is basically
    trying to find the exact address (the starting var
    in the arg list) to push on the stack. The va_* macros
    do this by using a far(32-bit) pointer. "Hacking" code
    together should [b]never[/b] be done in an actual project,
    and is not recommended.

    I never done it, but it is probably possible with the
    above method. I dont think its ever necessary though.
    If you are interested in trying to reverse the algorithms,
    here are the va_* macros... (with comments-im a little
    guessng here :-)
    [/blue][code]
    [blue]//far pointer to varable list[/blue]
    typedef void _FAR *va_list;

    [blue]//number of args in the list (sizeof ptr=sizeof int)[/blue]
    #define __size(x) ((sizeof(x)+sizeof(int)-1) & ~(sizeof(int)-1))

    #define va_dcl va_list va_alist;[blue]//not sure..[/blue]

    [blue]//sets va_list* to addr of var [b]before[/b] arg list[/blue]
    #define va_start(ap) ap = (va_list)&va_alist

    [blue]//extracts arg from list[/blue]
    #define va_arg(ap, type)(*(type _FAR *) /
    (((*(char _FAR * _FAR *)&(ap))+=__size(type))-(__size(type))))

    [blue]// nulls out the end element of the ap list[/blue]
    #define va_end(ap) ap = ((void _FAR *)0)
    [/code]
    [blue]
    As I said before, I recommend not to worry about this.
    I dont think you'll ever encounter a situation which needs
    this. If you do, then redesign!

    Good Luck!
    [blue]
    [/size]


  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : What I ment by "Brute-Force Hacking" is basically
    : trying to find the exact address (the starting var
    : in the arg list) to push on the stack. The va_* macros
    : do this by using a far(32-bit) pointer. "Hacking" code
    : together should [b]never[/b] be done in an actual project,
    : and is not recommended.
    :
    : I never done it, but it is probably possible with the
    : above method. I dont think its ever necessary though.
    : If you are interested in trying to reverse the algorithms,
    : here are the va_* macros... (with comments-im a little
    : guessng here :-)
    : [/blue][code]
    : [blue]//far pointer to varable list[/blue]
    : typedef void _FAR *va_list;
    :
    : [blue]//number of args in the list (sizeof ptr=sizeof int)[/blue]
    : #define __size(x) ((sizeof(x)+sizeof(int)-1) & ~(sizeof(int)-1))
    :
    : #define va_dcl va_list va_alist;[blue]//not sure..[/blue]
    :
    : [blue]//sets va_list* to addr of var [b]before[/b] arg list[/blue]
    : #define va_start(ap) ap = (va_list)&va_alist
    :
    : [blue]//extracts arg from list[/blue]
    : #define va_arg(ap, type)(*(type _FAR *) /
    : (((*(char _FAR * _FAR *)&(ap))+=__size(type))-(__size(type))))
    :
    : [blue]// nulls out the end element of the ap list[/blue]
    : #define va_end(ap) ap = ((void _FAR *)0)
    : [/code]
    : [blue]
    : As I said before, I recommend not to worry about this.
    : I dont think you'll ever encounter a situation which needs
    : this. If you do, then redesign!
    :
    : Good Luck!
    : [blue]
    : [/size]
    SIR ,
    Thanks Again
    YEs I saw These THIngs In HEader FILEs TOO.
    BUt I would Rather Write " Hello WOrld! " than to
    Tax My mind with REversing THe Va`s in THis PArt
    Of My Tenure in Learning C.
    Very Soon i will be bothering you with the questions of
    Programming on DISk FIles FAT and Stuff
    Because I know now How to Write CreateFile();

    THe First ALphabet One needs to type is C THen R And THEn
    E ANd So On ....

    enough of my non-sense.
    THanks A billion FOr your TIme.




Sign In or Register to comment.