Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 24th, 2007, 05:47 PM
Authorized User
 
Join Date: Dec 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default char* argv to main - counting and termination

Should command line arguments have a null termination at the end?

The book I'm studying (Ivor Horton's Beginning C++) says they will, but my test of the code in his book shows that the null is not there.

Interestingly, in viewing the hex editor while debugging, I see that there is a "0" between each char array. For example, my command line is such:

"c:\myProgram.exe help me debug this"

then the char* array arguments, (char* argv[]) shows like this in the hex editor:
c:\myProgram.exe0help0me0debug0this0

But none of these zeros are detectable in my loop, which looks for the 0 value to end looping.

corn-fused in Indiana,
Brian

code here:
Code:
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
  cout << endl;
  int i = 0;
  while(argv[i] != '\0')
    cout << (i+1) << ": " << *argv[i] << " and string: " << argv[i++] << endl;
  cout << "argc = " << argc << endl;
  cout << "sizeof char* argv[]= " << (sizeof argv) << endl;
  return 0;
}
Sincerely,
Brian
__________________
Sincerely,
Brian
Reply With Quote
  #2 (permalink)  
Old March 4th, 2007, 01:22 AM
Registered User
 
Join Date: Mar 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The cmd line args are passed to Main as an "array of pointers to cstrings". Therefore your code should be something like this to find the 0.

for ( int i=0; i< argc; i++)
{
  char* arg = argv[i];
  while(*arg)
  {
    arg++;
  }
  // arg now points to '\0'
}



Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ARGV in Perl Freedolen Perl 18 October 18th, 2012 06:36 PM
zombie to exist after termination of main prog. anilchowdhury Need help with your homework? 0 February 22nd, 2008 02:56 PM
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
char* argv in main() - counting and termination proslambano BOOK: Ivor Horton's Beginning Visual C++ 2005 1 March 14th, 2007 04:33 PM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.