Thread: Getting the size of **argv.

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Getting the size of **argv.

    No matter what I try to do here, I always come out with a result of 4 when I do a sizeof(argv[1]). No matter how large or small the input is on the command line, the sizeof always returns 4.

    My question now is, how can I get the size of the array that holds the char data from the input of the command line?

    Some code I tried:

    Code:
    int main(int argc, char **argv)
    {
    	int x = sizeof(argv[1]);
    	std::cout << x << "\n";
    
    	x = sizeof(argv);
    	std::cout << x << "\n";
    
    	return(0);
    }
    And the output? 4 on both lines. It's starting to confuse me.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    #include <cstring> and use strlen( argv[1] ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by XSquared
    #include <cstring> and use strlen( argv[1] ).
    *looks dumbfounded*

    How'd I forget that? Heh.. Thank you!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The reason you were getting 4 as a result, was that you were essentially performing sizeof(char*), which of course will be 4 on a 32 bit machine

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well 4 on common 32-bit desktop machines at least
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by bithub
    The reason you were getting 4 as a result, was that you were essentially performing sizeof(char*), which of course will be 4 on a 32 bit machine
    That makes sense. Thanks for the explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM