473,231 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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

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
Jan 5 '07 #1
7 4753
On Thu, 4 Jan 2007 21:22:01 -0800, Vin <Vi*@discussions.microsoft.com>
wrote:
>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.
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

--
Doug Harrison
Visual C++ MVP
Jan 5 '07 #2
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
"Vin" <Vi*@discussions.microsoft.comescribió en el mensaje
news:80**********************************@microsof t.com...
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

Jan 5 '07 #3
Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]
Hi,
That could be the problem. indexing in arrays is 0 based. meaning the first
element has index 0, and the last element has index n-1.
if n is the size of your array, indexing should be done
argv[0]...argv[n-1]

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jan 5 '07 #4
Vin wrote:
Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]

But what's up with the @# and where did it come from?
If you address an item outside of the array's memory space, such things
are expected to happen. Nobody knows where it's coming from, that's just
the content of your memory -- garbage, or the content of other objects,
perhaps leftover by the operating system or the debugger. You have no
control over that. Once you address an invalid pointer, the consequences
are undefined and somewhat random.
It will only show up for char* argv[] and not for char** argv
As others have already stated it, there's no difference between those
two types when they are used in function arguments.

If you don't believe it, cut and paste these 2 definitions:
void f(char* argv[]) { }
void f(char** argv) { }

You'll get a compiler error, something like "body has already been
defined for 'f(char**)'". They violate the One Definition Rule, because
they're identical.
for char* argv[]: Another weird thing is that at times it will have this @#
and at other times it doesn't.
See, this just proves the random nature of this behavior. It doesn't
depend on whether the type is char*[] or char**, but on circumstances
beyond your control. Due to a programming error, you're exploiting
undefined behavior. I have no question in my mind that when you use that
array properly, it will return the correct value.

Tom
Jan 5 '07 #5
On Fri, 5 Jan 2007 00:15:00 -0800, Vin <Vi*@discussions.microsoft.com>
wrote:
>Hi,

Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]

But what's up with the @# and where did it come from?

It will only show up for char* argv[] and not for char** argv

for char* argv[]: Another weird thing is that at times it will have this @#
and at other times it doesn't. Also if I execute the program on another
computer it never show up all.

Any ideas?
If you're using argv[n] for n argc, the result is undefined, which means
anything is possible. You can compile with /FAs and compare the assembly
code generated by the compiler, but if the only difference is char** vs.
char*[], I'd expect it to be the same. How are you determining what the
strings contain? Show the code.

--
Doug Harrison
Visual C++ MVP
Jan 5 '07 #6
Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]

But what's up with the @# and where did it come from?
If you go beyond the boundaries of the array, you get undefined values.
I.e. argv[n] returns something, but whatever it is is not subject to logical
behavior. You just get whatever value happens to be after argv[n-1]

That value is then interprested as a char pointer. That pointer can point
anywhere, since is is not a real pointer.
When you try to use that pointer to read a string, the string function will
start at that pointer, and search until it finds a 0 that indicates the end
of the string.

If you are lucky, it will find a 0 and use a bogus string.
if you are unlucky, it will continue searching until it hits an inaccessible
addres, at which time you will get an access violation.

since this is undefined behavior, you cannot compare what happens on 2
different computers, or 2 different runs of the application.
Your result depends on whatever happens to be in argv[n] when you read it.

--
Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Jan 8 '07 #7
In article <E3**********************************@microsoft.co m>,
=?Utf-8?B?Vmlu?= <Vi*@discussions.microsoft.comwrote:
>Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]
C/C++ count arrays from 0, so that the array boundaries should be
argv[0] .. argv[n-1]. Also, C/C++ tends to treat array sizes as merely
suggestions, not hard limits. You can invite a lot of security
problems in your programs if you don't program defensively. C/C++
tends to give you all the tools you need to shoot yourself in the
foot-- and offers you a bigger bullet. This gives them a lot of power,
but if you can't handle that power properly, you might want to
consider another language.

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Jan 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in...
9
by: Vinu | last post by:
Hai when i compile the cpp file(cmdargs.cpp) i attached the output below the program int main(int argc, wchar_t* argv) { std::wcout<<L"Name of the Program is "<<*argv<<std::endl;...
14
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
1
by: mudanoman | last post by:
Hello, I am new to c and need help with a current program I am working on. Currently, the program (code below) input is as follows: program apple.test.com 51112 What I need is...
9
by: pkirk25 | last post by:
int main(int argc, char *argv) { int z = 0; cout << *argv << endl; return z; } tester hi james outputs the letter 'j'
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
3
by: Bill Cunningham | last post by:
I have been having a little trouble with this page. http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line I wanted to create a command called div that takes two...
2
by: significantBit | last post by:
n00b here. Just started learning C a couple of days ago. I'm using xcode alternatively with emacs. My question is in regards to the main function. Everytime I create a project (standard...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.