int main(int argc, char* argv[]) & int main(int argc, char** argv)

int main(int argc, char* argv[]) & int main(int argc, char** argv)

Post by Vmlu » Sat, 06 Jan 2007 14:22:01


Hi,

I have a question. I created a simple executable program using Visual C++
from Visual Studio 6.0

This program is called from a script that passes in one argument.

Now, my question is:

1. When I use int main(int argc, char* argv[]) declaration, the argv usually
contains the data and some garbage at the end.

For example: a script calls this executable and passes in data => this-is-me
When it executes this C++ program, the argv contains => this-is-me@#

Where did the @# come from? and Why?

2. When I use main(int argc, char** argv) declaration, the argv is exactly
what the script passes it.

For example: a script calls this executable and passes in data => this-is-me
When it executes the C++ program, the argv contains exactly => this-is-me

So, what is the difference between these two declarations? Why does the
first one contains garbage characters?

Please help me to understand. I am fairly new at Visual C++.

Many Thanks
 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** argv)

Post by Doug Harri » Sat, 06 Jan 2007 15:53:29

On Thu, 4 Jan 2007 21:22:01 -0800, Vin < XXXX@XXXXX.COM >



I don't understand how that can be. For function parameter declarations,
and only function parameter declarations, char** and char*[] mean the same
thing, pointer-to-pointer to char. Similarly, and again, in only this
context, char* and char[] mean the same thing, which is pointer to char.


There is no difference. So there necessarily is some other difference,
which could be a compiler bug, though I cannot recall ever hearing of a
compiler bug such as this. How are you determining what the strings
contain?


If you are a newcomer to C++, don't feel bad, because arrays and pointers
are not the same thing at all, even though the syntax sometimes disagrees,
all in the interest of helping you declare things the way the language
designer thought you'd use them. The C FAQ has a lot of good information on
the subject:

6. Arrays and Pointers
http://www.yqcomputer.com/

--
Doug Harrison
Visual C++ MVP

 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** argv)

Post by Cholo Lenn » Sat, 06 Jan 2007 16:06:27

How do you access the arguments in both cases? Can you show some code?
The correct way with char argv[] is:

int main(int argc, char argv[])
{
if (argc>1)
printf("1st program argument=%s\n", argv[1]);
else
printf("Program %s is running without arguments\n", argv[0]);
}




"Vin" < XXXX@XXXXX.COM > escribien el mensaje
>> Hi, >> >> I have a question. I created a simple executable program using Visual C++ >> from Visual Studio 6.0 >> >> This program is called from a script that passes in one argument. >> >> Now, my question is: >> >> 1. When I use int main(int argc, char* argv[]) declaration, the argv usually >> contains the data and some garbage at the end. >> >> For example: a script calls this executable and passes in data >> this-is-me >> When it executes this C++ program, the argv contains >> this-is-me@# >> >> Where did the @# come from? and Why? >> >> 2. When I use main(int argc, char** argv) declaration, the argv is exactly >> what the script passes it. >> >> For example: a script calls this executable and passes in data >> this-is-me >> When it executes the C++ program, the argv contains exactly >> this-is-me >> >> So, what is the difference between these two declarations? Why does the >> first one contains garbage characters? >> >> Please help me to understand. I am fairly new at Visual C++. >> >> Many Thanks