Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 24th, 2007, 04:47 PM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 34
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, 12:22 AM
Registered User
 
Join Date: Mar 2007
Location: San Jose, CA, USA.
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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01:56 PM
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 08: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 06:56 AM



All times are GMT -4. The time now is 08:01 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.