ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 08:59 AM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.