LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

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


Hi,

Kindly see the code below

Code:
#include "stdio.h"

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

o/p
---
size of argv = 4

Now see the second piece of code

Code:
#include "stdio.h"

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



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

waiting for reply

Regards,
thelink
 
Old 09-01-2009, 09:58 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

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

change the code in your second example to read
Code:
#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.

EDIT:

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 09:59 AM.
 
Old 09-01-2009, 10:05 AM   #3
karamarisan
Member
 
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.
 
  


Reply

Tags
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 07:47 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 02:51 PM
Perl command line argument noir911 Programming 8 01-15-2008 05:59 AM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 10:51 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 05:35 PM


All times are GMT -5. The time now is 01:22 PM.

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