View Single Post
  #1 (permalink)  
Old February 24th, 2007, 05:47 PM
proslambano proslambano is offline
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