echo help

i found this code in the book but was unable to execute it.
/*myecho -- echo command line arguments */

main(int argc, char **argv)
{
while(--argc > 0)
printf("%s ", *++argv);
printf("\n");
}

this is another program that would invoke the one above

char * const argin[] = {"myecho", "hello", "world", (char *)0};

execvp(argin[0], argin);

the output suppose to display "hello world"

i tried putting the bottom portion in another main function, but it did not work.
help please. thank you

The code you posted is legal although it is not very well written. I too put the last fragment inside a main function.

As written, the code assumes that the myecho program will be found on the PATH. Since that would not be true in my case, I changed the {"myecho", to read {"./myecho",

Other than the PATH search issue, I don't see anything wrong enough to keep it from running.

What system are you using? What compiler? What result do you get when you try this?

i'm using gcc.
compiling on a remote computer using ssh.
the first main program i saved it as myecho.c
the second main program(the bottom portion) i called it caller.c
when i gcc caller.c then a.out, i didn't get anything. It's like it's not calling anything.
o...and ./myecho doesn't make any difference
thanks

Works fine with gcc, too. Do:
gcc myecho.c -o myecho
./myecho test one two three
gcc caller.c -o caller
./caller
uname -a

and post the results.

a ha...i found the problem. instead of "myecho" i typed it as "myecho.c".
hey thanks alot for taking the time to answer.
by the way, i didn't run it as ./ i jut runned it regularly and it still worked. instead of ./a.out i just simply used a.out and it run just fine. what's the point of using ./ ?
thanks

That must mean that "." is in your PATH, or you are in a directory that is in your path. "./program" will execute the "program" in your current directory.

Many people will remove "." (the current directory) from their PATH variable for security reasons, and others will do it just to be safe. Many normal users can leave "." in their PATH, as long as they're cautious of where they are and what they're executing.

Also, if you name a program "test", you'll want to give a full path to it, since "test" already exists in many shells as a builtin command, or as a system binary. In that case executing "test" and executing "./test" will be two different programs.