tags:

views:

810

answers:

5

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

How does this work?

I'd really ( really ) like to know!

const char *cPtr = argv[1];

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

Is this possible?

+8  A: 

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.

paxdiablo
A: 

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

Per Stilling
+1  A: 

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);
}
DasBoot
+2  A: 

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;
}
m-sharp
OMG! My eyes are bleeding! :-)
paxdiablo
Indeed. It's non-portable!
greyfade
Yeah, but not until the first line :-)
paxdiablo
Dude, that's the most intrincate code I've seen in years. It makes the use of a global variable look so clean.
Marc
He did ask how to pass a parameter to a function that takes no params... that got me thinking :). I agree it's nasty code but it is fits the question.
m-sharp
A: 

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;
}
hamishmcn