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.
Ok, so I'm reading the C programming language, 2nd ed by Kernighan and Ritchie and I decide to compile this bit of code from part "5.10 Command line arguments":
$ gcc -o pointerarray2 pointerarray2.c
pointerarray2.c: In function ‘main’:
pointerarray2.c:10: error: lvalue required as increment operand
WHY? Isn't "char *myarray[]" the same as "char *argv[]" ?? Is this just whimsy compiler design or what is it? I mean, I could use argv[n] or something to scan through the array but my aim is not to get it working, but to understand why ANSI C makes this difference.
When char *something[] is the point of definition of an array, the symbol something is the value indicating the location of the array; the value itself has no location (i.e. the compiler inserts the value where it's needed,) which makes it an rvalue. When you use char *something[] as a function argument, you indicate that you're expecting a pointer to an array of fixed size (it's really taken as char **something); to do this, a pointer to the array must be pushed to the stack, which makes it an lvalue, which can be incremented.
Kevin Barry
Is this just whimsy compiler design or what is it?
Certainly it is nothing to be blamed on the compiler design. It is a questionable choice in the language design, and one that confuses most beginning C programmers.
To try to clarify Kevin's correct answer, consider this program
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.