user40120
user40120

Reputation: 648

How can I pass argv[1] to a function that takes 0 arguments?

What exactly do I need to do to get the contents of argv[1] to a function that uses no arguments?

How does this work?

const char *cPtr = argv[1];

and pass it to someFunction() that takes 0 arguments!

Upvotes: 0

Views: 4273

Answers (5)

hamishmcn
hamishmcn

Reputation: 7981

It would help if we understood more of why you want to pass an argument to a function that takes no parameters.
For example are you trying to apply a function to the items in a container?

Others have mentioned global variables, here is an approach that "cheats" by passing the argument to a constructor of a class or structure:

struct fnstruct { 
    const char* p_; 
    fnstruct(const char* p) : p_(p) { }
    void operator()() { printf(p_); }
};

int main(int argc, char* argv[]) {
    if(argc < 2)
        return 0;

    fnstruct fn(argv[1]); //argument passed to constructor

    fn(); //function called with no argument!
    return 0;
}

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882386

If you don't want to pass it as an argument, you'll need to shove it in a global so that the function can access it that way. I know of no other means (other than silly things like writing it to a file in main() and reading it in the function):

static char *x;
static void fn(void) {
    char *y = x;
}
int main (int argc, char *argv[]) {
    x = argv[1];
    fn();
    return 0;
}

This is not how I would do it. Since you need to be able to change fn() to get access to x anyway, I'd rewrite it to pass the parameter:

static void fn(char *x) {
    char *y = x;
}
int main (int argc, char *argv[]) {
    fn(argv[1]);
    return 0;
}

Unless, of course, this is homework, or a brainteaser, or an interview question.

Upvotes: 9

m-sharp
m-sharp

Reputation: 17125

I wonder why you want to do this. But it can be done using some not so pretty code. Essentially call the method as if it were a method taking the char* parameter. Then in the method, use inline assembly to access the parameter. Here is an example with the someFunction implemented with the __cdecl calling convention.

#include "stdafx.h"

void someFunction()
{
    TCHAR *x = NULL;
    __asm 
    {
        mov eax, dword ptr[ebp + 8]
        mov x, eax
    }

#ifdef _UNICODE
    printf("%ws\n", x);
#else
    printf("%s\n", x);
#endif
}


int _tmain(int argc, TCHAR* argv[])
{
    if (argc < 2)
        return 1;

    const TCHAR *message = argv[1];

    typedef void (*FuncPtr)(const TCHAR*);

    FuncPtr p = (FuncPtr)someFunction;

    p(message);

    return 0;
}

Upvotes: 3

Himadri Choudhury
Himadri Choudhury

Reputation: 10353

Only way I can think of is to make "const char *cPtr" a global variable.

e.g.

char *cPtr;

main(int argc, char **argv) 
{
    cPtr = argv[1];
} 
void someFunction()
{
  printf("%s\n", cPtr);
}

Upvotes: 1

Per Stilling
Per Stilling

Reputation: 876

The only way to do this would be to store argv[1] in a global variable and access it from within the function.

Upvotes: 0

Related Questions