Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vin - 05 Jan 2007 05:22 GMT
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
Doug Harrison [MVP] - 05 Jan 2007 06:53 GMT
>Hi,
>
[quoted text clipped - 15 lines]
>2. When I use main(int argc, char** argv) declaration, the argv is exactly
>what the script passes it.

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.

>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?

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?

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

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://c-faq.com/aryptr/index.html

Signature

Doug Harrison
Visual C++ MVP

Cholo Lennon - 05 Jan 2007 07:06 GMT
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]);
}

--
Cholo Lennon
Bs.As.
ARG

> Hi,
>
[quoted text clipped - 25 lines]
>
> Many Thanks

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.