Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-01-2009, 08:18 AM   #1
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Smile Program on Command Line Argument and Pointers


Kindly see the code below

#include "stdio.h"

int main(int argc,char *argv[])
   printf("size of argv = %d\n",sizeof(argv));
   return 0;
./temp one two

size of argv = 4

Now see the second piece of code

#include "stdio.h"

int main(int argc,char *argv[])
   char *ptr[] = {"hi","hello"};
   printf("size of ptr = %d\n",sizeof(ptr));
size of ptr = 8

argv and ptr are both array of character pointers. Then why the difference in their sizes occur?

waiting for reply

Old 09-01-2009, 08:58 AM   #2
Registered: May 2002
Location: dracut MA
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
Here's a hint:

change the code in your second example to read
#include <stdio.h>
int main(int argc, char *argv[])
   char *ptr[] = {"hi", "hello", "goodbye"};
   printf("elem_test: %d ???\n", (sizeof(ptr)/sizeof(char *)));
   return 0;
Then keep adding elements to ptr and see what your results might be telling you.


Ahh, I understand the question now.
The reason for the discrepancy is because sizeof() is a compile-time function (it's replaced by the compiler with the "correct" information). In the case of ptr, the compiler knows how big it is. In the case of argv, the compiler can't, so it just gives you what the size of a pointer would be.

Last edited by orgcandman; 09-01-2009 at 08:59 AM.
Old 09-01-2009, 09:05 AM   #3
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374

Rep: Reputation: 55
It's a matter of what the compiler knows, as sizeof() calls are resolved at compile-time. C doesn't really have much of a conception of arrays - really, just pointers. Normally, what that means is that the programmer is entirely responsible for keeping track of how many bars are stored at bar* foo. sizeof(argv) == sizeof(char**), which is just a pointer. You're obviously on a 32-bit system, so that's 4. Because argv isn't defined until run-time, there's no way the compiler could give you any other value in good faith. (Note that a pointer to an array is a pointer to a pointer and nothing more - int a[] is a pointer to an int, and can be used in exactly the same way.)

Now, for your ptr variable - because it is declared and defined in one line like that, it ends up in a different part of the binary used for fixed data (.data, I think, though I'm not sure). Since, at least at that point in your program, ptr refers to an array literal (if you will), the compiler tries to be helpful and give you the size of what's at ptr, which is 2x char* (&"hi", &"hello"). Kinda makes sense - I guess it's an easy way to have the size of an array in your code change dynamically as you change the array.


argument, command, line, pointer

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
command line argument in c vipin_jss Linux - Newbie 6 05-11-2009 06:47 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
Perl command line argument noir911 Programming 8 01-15-2008 04:59 AM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration